[英]Angular with {{}} directive attribute
有什么方法可以将{{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}}
。
在触发初始摘要周期之前,您将无法访问$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.
});
}
}
})
这是一种很好且更专业的方法。在angularjs中,有一个关于范围字段类型的想法:
它有3种类型。
1)字符串@
2)功能与
3)双向=
这种样式更好。此链接可以为您提供帮助。
https://docs.angularjs.org/api/ng/service/ $ compile#directive-definition-object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.