Javascript AngularJS指令修改链接或编译函数中加载的模板
我正在指令中加载一个模板,我想根据一些给定的属性修改它。我该怎么做 这是我的指示:Javascript AngularJS指令修改链接或编译函数中加载的模板,javascript,angularjs,Javascript,Angularjs,我正在指令中加载一个模板,我想根据一些给定的属性修改它。我该怎么做 这是我的指示: directives.directive("myDirective", function($http, $compile){ return { restrict: 'E', scope : { }, templateUrl: 'lib/directives/myTemplate.html', link : function(scope, element, a
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所回答的那样,可以使用link/compile方法的元素参数访问template或templateUrl参数中设置的模板:
compile : function(element, attrs) {
}
link : function(scope, element, attrs) {
}
您总是可以加载另一个模板,编译它,在需要的地方追加。我尝试使用var template=$compile'lib/directives/ing table page.html',但参数无效。是你的意思吗?是的。但首先要使用$http.gettemplateUrl.thenfunctiontemplate{$compileangular.elementtemplatescope.appendTosomewhere}下载模板。我想是这样的。使用指令的编译阶段,有关更多信息,请参阅的编译部分。在这里,您可以修改您的DOM;