[英]Angular with {{}} directive attribute
Is there any way to pass {{string}}
to directive which gets this attribute from $attrs
(not $scope
)? 有什么方法可以将{{string}}
传递给从$attrs
(而不是$scope
)获取此属性的指令?
Here is some code: 这是一些代码:
controller: 控制器:
...
scope.someId = "someId";
...
HTML: HTML:
<my-dir id="{{someId}}"></my-dir>
Directive 指示
app.directive('myDir', function() {
return {
controller: function($attrs){
console.log($attrs.id) //output: {{someId}}
}
}
})
What I want is that the output would be someId
and not {{someId}}
. 我想要的是输出将是someId
而不是{{someId}}
。
You will not be able to access the value from $attrs
till the initial digest cycle is triggered. 在触发初始摘要周期之前,您将无法访问$attrs
的值。 Once the digest cycle is triggered, you will be able to access it from the $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.
});
}
}
})
Here is a good and more professional way to do it.In angularjs there is an idea about scope field type: 这是一种很好且更专业的方法。在angularjs中,有一个关于范围字段类型的想法:
There are 3 types of it. 它有3种类型。
1) string @ 1)字符串@
2) functional & 2)功能与
3) biDirectional = 3)双向=
This style is better.This link can help you. 这种样式更好。此链接可以为您提供帮助。
https://docs.angularjs.org/api/ng/service/ $compile#directive-definition-object https://docs.angularjs.org/api/ng/service/ $ compile#directive-definition-object
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.