[英]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.