访问 AngularJS 指令 templateUrl 中的属性
Access attrs in AngularJS directive templateUrl
我有一个问题,因为我还不能 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>
显然这确实有效,我的指令值被传递给 link 函数但没有传递给 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
}
};
}]);
我有一个问题,因为我还不能 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>
显然这确实有效,我的指令值被传递给 link 函数但没有传递给 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
}
};
}]);