У меня есть директива ("дочерняя"), вложенная в другую директиву ("родительскую"). Это require
s ngModel, а ngModelCtrl.$modelValue
отображается и поддерживается в актуальном состоянии в своем шаблоне. То есть, пока я не вызову ngModelCtrl.$setViewValue().
Итак, вот HTML, инициализирующий директивы:
<div parent>
<div child ng-model="content">Some</div>
</div>
А вот и директивы:
angular.module('form-example2', [])
.controller('MainCtrl', function($scope){
$scope.content = 'Hi';
})
.directive('parent', function() {
return {
transclude: true,
template: '<div ng-transclude></div>',
controller: function(){
},
scope: {}
};
})
.directive('child', function() {
return {
require: ['ngModel', '^parent'],
transclude: true,
template: '<div>Model: {{model.$modelValue}} (<a style="text-decoration: underline; cursor: pointer;" ng-click="alter()">Alter</a>)<br />Contents: <div style="background: grey" ng-transclude></div></div>',
scope: {},
link: function(scope, elm, attrs, ctrl) {
var ngModelCtrl = ctrl[0];
var parentCtrl = ctrl[1];
scope.model = ngModelCtrl;
// view -> model
scope.alter = function(){
ngModelCtrl.$setViewValue('Hi2');
}
// model -> view
// load init value from DOM
}
};
});
Когда модель (т.е. content
) изменяется, это изменение можно увидеть внутри дочерней директивы. Когда вы нажимаете ссылку «Изменить» (которая запускает вызов $setViewValue()), значение модели должно стать «Hi2». Это правильно отображается внутри директивы child
, но не в модели вне директивы. Кроме того, когда я теперь обновляю модель вне директивы, она больше не обновляется внутри директивы.
Как так?