[AngularJS] Angular 1.5 $transclude with named slot

In Angular 1.5, there is no link and compile. 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.

posted @   Zhentiw  阅读(351)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示