У меня есть директива с изолированной областью со значением с двухсторонней привязкой к родительской области. Я вызываю метод, который изменяет значение в родительской области, но это изменение не применяется в моей директиве. (Двухсторонняя привязка не запускается). Этот вопрос очень похож:
но я не изменяю значение из директивы, но меняю ее только в родительской области. Я прочитал решение, и в пункте 5 сказано:
The watch() created by the isolated scope checks whether it value for the bi-directional binding is in sync with the parent value. If it isn't the parent value is copied to the isolated scope.
Это означает, что когда мое родительское значение изменено на 2, запускается часы. Он проверяет, одинаково ли значение родительского и директивного значений, и если оно не копирует значение директивы. Хорошо, но мое значение директивы все равно 1... Что мне не хватает?
html:
<div data-ng-app="testApp">
<div data-ng-controller="testCtrl">
<strong>{{myValue}}</strong>
<span data-test-directive data-parent-item="myValue"
data-parent-update="update()"></span>
</div>
</div>
js:
var testApp = angular.module('testApp', []);
testApp.directive('testDirective', function ($timeout) {
return {
scope: {
key: '=parentItem',
parentUpdate: '&'
},
replace: true,
template:
'<button data-ng-click="lock()">Lock</button>' +
'</div>',
controller: function ($scope, $element, $attrs) {
$scope.lock = function () {
console.log('directive :', $scope.key);
$scope.parentUpdate();
//$timeout($scope.parentUpdate); // would work.
// expecting the value to be 2, but it is 1
console.log('directive :', $scope.key);
};
}
};
});
testApp.controller('testCtrl', function ($scope) {
$scope.myValue = '1';
$scope.update = function () {
// Expecting local variable k, or $scope.pkey to have been
// updated by calls in the directive scope.
console.log('CTRL:', $scope.myValue);
$scope.myValue = "2";
console.log('CTRL:', $scope.myValue);
};
});
Fiddle: http://jsfiddle.net/u69PT/17/