有没有办法将{{string}}
传递给指令,该指令从$attrs
(而不是$scope
)获取此属性?
以下是一些代码:
控制器:
...
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}}
。
答案 0 :(得分:1)
在触发初始摘要周期之前,您将无法访问$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.
});
}
}
})
答案 1 :(得分:0)
这是一个很好的,更专业的方法。在angularjs中有一个关于范围字段类型的想法:
它有3种类型。
1)string @
2)功能性&amp;
3)biDirectional =
这种风格更好。这个链接可以帮到你。
https://docs.angularjs.org/api/ng/service/ $编译#指令定义对象