码迷,mamicode.com
首页 > Web开发 > 详细

[AngularJS] Angular 1.5 $transclude with name slot

时间:2016-04-13 00:06:38      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:

In Angular 1.5, there is no link and component. So use if you transclude, you cannot access the fifth arguement in link function, which is transcludeFn.

 

But in v1.5, you can access $transclude in controller. 

And what you can do for that is check whether the transclude element is passed in or not. For that you also need to use named slot, not default transclude: true.

 

See example:

class ServiceListController {
    constructor($transclude) {
        this.$transclude = $transclude;
    }

    hasTransclude(){
        return this.$transclude.isSlotFilled(actions);
    }
}

const clmServiceListComponent = {
    bindings: {
       ...
    },
    transclude: {
        actions: ?clmActions
    },
    controller: ServiceListController,
    controllerAs: vm,
    template: require(./service-list.html)
};

export default (ngModule) => {
    ngModule.component(clmServiceList, clmServiceListComponent);
}
    

In the example, we has a directive call ‘clmActions‘, and gave slot name as ‘actions‘.

So you can use:

this.$transclude.isSlotFilled(actions);

to check whether the transclude element is defined or not.

 

In HTML:

<clm-service-list>
      <clm-actions>
           <clm-action ></clm-action>
       </clm-actions>
</clm-service-list>

If we gave the clm-actions tag, the hasTransclude() function in controller will return ‘true‘, if you remove clm-actions tag, the function will return false.

[AngularJS] Angular 1.5 $transclude with name slot

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5384874.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!