Detect if a transclude content has been given for a angularjs directive

前端 未结 4 2149
小鲜肉
小鲜肉 2020-12-10 00:19

I have a directive (a progressbar) which should have two possible states, one without any description and one with a label on the left side. It would be cool to simple use

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-10 01:11

    After release of Angular v1.5 with multi-slot transclusion it's even simpler. For example you have used component instead of directive and don't have access to link or compile functions. Yet you have access to $transclude service. So you can check presence of content with 'official' method:

    app.component('myTransclude', {
      transclude: {
        'slot': '?transcludeSlot'
      },
      controller: function ($transclude) {
        this.transcludePresent = function() {
          return $transclude.isSlotFilled('slot');
        };
      }
    })
    

    with template like this:

    ...

提交回复
热议问题