我有一个问题,因为我尚未100%理解AngularJS指令。我有这样的指令,
在我的HTML中:
<div my-directive="Hello World"></div>
在我的指令JS文件中:
.directive('myDirective', [function ( ) {
return {
restrict: 'A',
priority: 100,
templateUrl: 'views/templates/my-directive.html',
link: function (scope, element, attrs) {
// I wish to pass attrs.myDirective to the templateUrl
}
};
}]);
并在我的templateUrl'views / templates / my-directive.html'中:
<h1> {{ attrs.myDirective }} </h1>
显然,这确实有效,我的指令值传递给了链接函数,但没有传递给templateUrl,如何在我的templateUrl中输出该值?我知道一个愚蠢的问题,但我不确定实现此目标的最佳方法是什么?
您可以简单地将此值分配给作用域变量。
scope.myDirective=attrs.myDirective
app.directive('myDirective', [function ( ) {
return {
restrict: 'A',
priority: 100,
templateUrl: 'my-directive.html',
link: function (scope, element, attrs) {
scope.myDirective=attrs.myDirective
// I wish to pass attrs.myDirective to the templateUrl
}
};
}]);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句