我正在尝试将我的模板从内联移动到它自己的文件中。在我从模板更改为 templateUrl 之前一切正常
Glenn.directive('test', function($compile) {
return {
restrict: 'A',
priority: 1000,
terminal: true,
templateUrl: function(tElement, tAttrs) {
return ('test.html');
},
link: function(scope, element, attrs) {
attrs.$set('editable-text', 'content.' + attrs.edit + '.data');
attrs.$set('edit', null);
$compile(element)(scope);
}
}
});
test.html
{{ 'content.' + tAttrs.edit + '.data' }}
<button ng-click="' + tAttrs.edit + '_form'+ '.$show()" ng-hide="' + tAttrs.edit + '_form'+ '.$visible">edit</button>
为什么 tAttrs
没有被传递到我的模板 test.html?
最佳答案
我从来没有见过这种情况,我总是将一个字符串传递给 templateUrl 属性,例如
...
templateUrl: './foodirective.tmpl.html'
...
您可以在链接函数中分配指令元素中的属性:
myApp.directive('fooDirective', function(){
return{
restrict: 'E',
scope: true,
templateUrl: './foodirective.tmpl.html',
link: function(scope, elem, attrs){
// do stuff
scope.tAttrs = attrs;
}
}
})
我准备了一个 plunk给你。
关于javascript - AngularJS 指令通过 templateUrl 函数传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24918038/