[英]AngularJS directive modify loaded template in link or compile function
我正在指令中加載模板,我想根據一些給定的屬性對其進行修改。 我怎樣才能做到這一點 ?
這是我的指令:
directives.directive("myDirective", function($http, $compile){
return {
restrict: 'E',
scope : {
},
templateUrl: 'lib/directives/myTemplate.html',
link : function(scope, element, attrs) {
// I need template to contain myTemplate.html code, how can I do that ?
var trThead = template.find("thead").find("tr");
var headers = scope.attrs["headers"];
for (var i = 0; i < headers.length; i++) {
trThead.append('<th><span class="th-sort">' + headers[i] + '</span></th>');
}
element.append(template);
}
};
});
我不想直接在指令中定義HTML代碼,因為它太大了。
正如naeramarth7回答的那樣,可以使用鏈接/編譯方法的element參數來訪問template或templateUrl參數中設置的模板 :
compile : function(element, attrs) {
}
link : function(scope, element, attrs) {
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.