在查看AngularJS(和相关)文档以及有关指令中隔离范围的其他stackoverflow问题之后,我仍然有点困惑.为什么我不能在父作用域和指令隔离作用域之间进行双向绑定,其中父作用域属性是对象而不是属性?我应该只使用所需的财产scope.$parent吗?这似乎是错的.在此先感谢您的帮助.
相关的小提琴就在这里.
HTML:
<div ng-app="myApp">
<div ng-controller="myCtrl">
<div my-directive>{{test.name}}</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
JavaScript的:
var myApp = angular.module('myApp', []);
myApp.controller('myCtrl', function ($scope) {
$scope.test = {name:"name", value:"value"};
});
myApp.directive("myDirective", function () {
return {
replace: true,
restrict: 'A',
scope: {test: '='},
template: '<div class="parent"><div>This is the parent Div.</div><div>Value={{test}}</div></div>',
link: function (scope, element, attrs) {
console.log("scope.test=["+scope.test +"]");
console.log("scope.$parent.test=["+scope.$parent.test.name+"]");
}
};
});
Run Code Online (Sandbox Code Playgroud)