[英]AngularJS - How can I read the value of a model defined in an isolate scope?
我需要在指令中使用隔離作用域; 我正在嘗試讀取屬性中指定的模型的值。 所以我的指令是這樣使用的:
<div mydirective="" mydirective-data="MyJson" />
在控制器中,我為$scope.MyJson
分配了一個值。 我的指令應該選擇它,但是不可以。
app.directive('mydirective', function() {
return {
restrict: 'A',
scope: {
data: '&mydirectiveData',
},
link: function(scope, element, attrs) {
console.log(scope.data);
}
}
}
);
請注意,我需要使用隔離范圍。 我還創建了一個JSFiddle,其中包含此問題。 (記住要打開控制台)
我希望看到MyJson
的價值,但是卻什么也沒看到。
將范圍內的&
替換為=,它將為您提供對象。 如果您使用&
使用scope.data()
我會這樣寫指令:
app.directive('mydirective', function() {
return {
restrict: 'A',
scope: {
mydirectiveData: '='
},
link: function(scope, element, attrs) {
console.log(scope.mydirectiveData);
}
}
}
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.