[英]AngularJS - How can I read the value of a model defined in an isolate scope?
I need to use an isolate scope in a directive; 我需要在指令中使用隔离作用域; I am trying to read the value of a model specified in an attribute. 我正在尝试读取属性中指定的模型的值。 So my directive is used like this: 所以我的指令是这样使用的:
<div mydirective="" mydirective-data="MyJson" />
In a controller, I am assigning a value to $scope.MyJson
. 在控制器中,我为$scope.MyJson
分配了一个值。 My directive should pick it up, but doesn't. 我的指令应该选择它,但是不可以。
app.directive('mydirective', function() {
return {
restrict: 'A',
scope: {
data: '&mydirectiveData',
},
link: function(scope, element, attrs) {
console.log(scope.data);
}
}
}
);
Note that I need to use an isolate scope. 请注意,我需要使用隔离范围。 I have also created a JSFiddle with this problem in it. 我还创建了一个JSFiddle,其中包含此问题。 (Remember to open the console) (记住要打开控制台)
I expect to see the value of MyJson
, but am seeing nothing instead. 我希望看到MyJson
的价值,但是却什么也没看到。
Replace the &
within the scope with =, and it would get you the object. 将范围内的&
替换为=,它将为您提供对象。 If you are using &
use scope.data()
如果您使用&
使用scope.data()
I would write my directive this way: 我会这样写指令:
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.