Javascript AngularJS指令';的隔离作用域丢失对父级的引用
更新: 我找到了一个解决办法。在父范围中,我创建了一个对象,然后将对象的属性绑定到输入指令,如下所示:Javascript AngularJS指令';的隔离作用域丢失对父级的引用,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,更新: 我找到了一个解决办法。在父范围中,我创建了一个对象,然后将对象的属性绑定到输入指令,如下所示: //Parent controller $scope.data = {}; //Parent directive template template: '<custom-input model="data[\'test\']"></custom-input>', 这两个作用域都会收到有关此更改的通知,因此我认为最初会进行绑定,但当在子作用域中重新分配模型时,父作用域将
//Parent controller
$scope.data = {};
//Parent directive template
template: '<custom-input model="data[\'test\']"></custom-input>',
这两个作用域都会收到有关此更改的通知,因此我认为最初会进行绑定,但当在子作用域中重新分配模型时,父作用域将丢失对变量的引用
这让我有点困惑,为什么在子范围中重新分配变量会破坏一切,这不是绑定的使用方式吗?显然,提供的代码不是我遇到问题的实际代码,不可能在这里发布,因此我可能遗漏了一些实际导致错误的内容
提前谢谢
app.controller('customInputController', ['$scope', function($scope) {
$scope.$watch('model', function(value){
console.log('Child controller model updated!');
});
//This function is called when a user has selected an object,
$scope.select = function(object) {
//I believe the error is hiding nearby
$scope.model = angular.copy(object);
}
}]);
app.directive('customInput', function() {
return {
restrict: 'E',
template: '...',
scope: {
model: '='
},
controller: 'customInputController'
}
});
app.controller('parentController', ['$scope', function($scope) {
$scope.$watch('test', function(value){
console.log('Parent controller test updated!');
});
}]);
app.directive('parent', function() {
return {
restrict: 'E',
template: '<custom-input model="test"></custom-input>',
controller: 'parentController'
}
});
$scope.test = { name: "test" };