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

前端 未结 4 2140
小鲜肉
小鲜肉 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 00:59

    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 demonstrating this approach with ng-if to set default content if nothing to transclude: http://plnkr.co/hHr0aoSktqZYKoiFMzE6

提交回复
热议问题