admin管理员组文章数量:1384326
How do you create a 2 way binding with a nested property in an isolate scope with dotted notation. I thought 'myObject.data': "=data"
would work, but it does not. I don't want to link everything in the myObject
object. I know I could do some sort of watch, but 'myObject.data'
seems cleaner.
.directive("myDirective", [function() {
return {
restrict: "E",
scope: {
'myObject.data': "=data"
},
link: function (scope, element, attrs) {
scope.myObject = {
data: "myValue"
};
}
};
}])
How do you create a 2 way binding with a nested property in an isolate scope with dotted notation. I thought 'myObject.data': "=data"
would work, but it does not. I don't want to link everything in the myObject
object. I know I could do some sort of watch, but 'myObject.data'
seems cleaner.
.directive("myDirective", [function() {
return {
restrict: "E",
scope: {
'myObject.data': "=data"
},
link: function (scope, element, attrs) {
scope.myObject = {
data: "myValue"
};
}
};
}])
Share
Improve this question
edited Jan 12, 2014 at 5:39
analyticalpicasso
1,9938 gold badges26 silver badges45 bronze badges
asked Jan 12, 2014 at 5:29
WarrenWarren
7557 silver badges20 bronze badges
2 Answers
Reset to default 8Isolated scopes are generally useful only with templates, they should not be used as a way to declare how you want your directive attributes to be interpreted. This is because most directives that don't have a template usually need the semantics of either a child scope or the direct scope of their environment.
In your case, you probably don't even need a $watch, because object references are what enable 2 way data binding, but without your full code I cannot be sure.
In case you want to know the translations for an isolated scope semantics to just a normal one:
@name -> attrs.name
=name -> $scope.$eval(attrs.name);
&name -> function() { return $scope.$eval(attrs.name); }
EDIT 2:
After your ment, I came up with this plunker. To preserve two way data binding you have to use a "." in your ng-model declaration. This is because two way data binding does not work for value types, since they are immutable. You can't change the value of 100 for example. You need to pass around a reference type object and hang the values you are changing off of it. Your desire to specify the full path to the value in the isolated scope definition is not possible based on the principles that two way data binding is made possible by.
Javascript:
angular.module('plunker', [])
.directive('twoWay', function() {
return {
restrict: 'E',
template: '<div><input ng-model="thing.name" type="text" /></div>',
scope: {
thing: "="
},
link: function(scope, element, attrs) {
}
};
})
.controller('MainCtrl', function($scope) {
$scope.data = {
name: "World"
};
});
HTML:
<body ng-controller="MainCtrl">
<p>Hello {{data.name}}!</p>
<two-way thing="data"></two-way>
</body>
What I use in these cases is the following:
.directive("myDirective", [function() {
return {
restrict: "E",
scope: {
data: "="
},
controller: function($scope){
$scope.dot = $scope //<--- here is the trick
}
};
}])
Then you can always change data
in the directive's scope from an inherited scope through dot.data = 'whatever'
without setting watchers.
Not very elegant but it works jsut fine in cases where you are not using the controller as
syntax and don't want a $parent
nightmare.
本文标签: javascriptIsolate Scope quotquot binding and doted notation AngularJSStack Overflow
版权声明:本文标题:javascript - Isolate Scope "=" binding and doted notation AngularJS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744529077a2610920.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论