有没有办法将 {{string}}
传递给从 $attrs
(而不是 $scope
)获取此属性的指令?
这是一些代码:
Controller :
...
scope.someId = "someId";
...
HTML:
<my-dir id="{{someId}}"></my-dir>
指令
app.directive('myDir', function() {
return {
controller: function($attrs){
console.log($attrs.id) //output: {{someId}}
}
}
})
我想要的是输出是 someId
而不是 {{someId}}
。
最佳答案
在触发初始摘要周期之前,您将无法从 $attrs
访问该值。一旦触发摘要周期,您就可以从 $attrs
访问它。
app.directive('myDir', function() {
return {
controller: function($attrs){
console.log($attrs.id) //output: {{someId}}
$attrs.$observe('id', function (newVal, oldVal) {
console.log(newVal, oldVal); // here you will be able to access the evaluated value.
});
}
}
})
关于javascript - 带有 {{}} 指令属性的 Angular,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29373610/