检测一个transclude含量已经给出了angularjs指令 [英] Detect if a transclude content has been given for a angularjs directive

查看:112
本文介绍了检测一个transclude含量已经给出了angularjs指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个指令(一个进度),其应该有两个可能的状态,一个没有任何说明,一个与在左侧的标签。
它会很酷简单的使用此标签的内容transcluded

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 the transcluded content for this label.

有谁知道我可以添加一个类取决于一个transclude内容是否已发出或不是我的指令?

Does anyone know how I can add a class to my directive depending whether a transclude content has been given or not?

所以我想补充一下:

<div class="progress" ng-class="{withLabel: *CODE GOES HERE*}">
    <div class="label"><span ng-transclude></span>
    <div class="other">...</div>
</div>

非常感谢!

推荐答案

根据@宜兰的解决方案,你可以使用这个简单的$ transclude函数知道是否有transcluded内容或没有。

Based on @Ilan's solution, you can use this simple $transclude function to know if there is transcluded content or not.

$transclude(function(clone){
    if(clone.length){
        scope.hasTranscluded = true;
    }
});

Plnkr证明这种方法的NG-如果设置默认的内容,如果没有什么transclude: http://plnkr.co/hHr0aoSktqZYKoiFMzE6

这篇关于检测一个transclude含量已经给出了angularjs指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆