簡體   English   中英

Angular指令可選templateUrl

[英]Angular directive optional templateUrl

因此,我有一個指令,它可以是屬性或元素。 當它是一個屬性時,我不想在聲明了它的現有元素內加載任何模板。 當它是一個元素時,我想加載給定的模板並填充指令的自定義標簽。

我試過了:

return {
  link: function(){...},
  templateUrl: function(element, attrs){
    url = "path/to/directive/template.html";

    // Checking if directive is used as an attribute here
    if(angular.isDefined(attrs.myDirective)){
       url = null; // Tried false, empty string, etc. but angular not happy with any of it
    }
    return url;
  }
}

任何想法如何實現這一目標?

您是否考慮過編寫兩個具有相同名稱的指令,並使用limit來指定不同的模板行為?

function directiveFactory(usesTemplate){

    return ['$timeout', function($timeout){
        var ddo = {
            link: function(scope, el,attr){
                $timeout(someThing, 1000) 
                //if you dont actually need to use a timeout, might 
                //i suggest scope.$applyAsync if your version of angular supports it?
            }
        }

        if(usesTemplate){
            ddo.restrict = 'E';
            ddo.templateUrl = 'path/to/template';
        } else {
            ddo.restrict = 'A';
        }

        return ddo;
    }];

}

module('somename').directive('someName', directiveFactory(true)).directive('someName', directiveFactory(false));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM