I'm having trouble understanding why changes to an ngModel doesn't propagate from one directive to another. Here's a plunker that shows a simplified version of what we're trying to do.
Basically, I've declared a directive which uses ngModel, and an isolate scope:
.directive('echo', function() {
var link = function(scope, element, attrs, ngModel) {
// --- 8< ---
scope.$watch(attrs['ngModel'], function() {
scope.model = ngModel.$modelValue;
console.log("***** Echo model updated: ", scope.model);
});
};
return {
restrict: 'E',
require: 'ngModel',
link: link,
scope: {
id: "="
}
}
})
That directive is then wrapped by another directive, also with a dependency on ngModel and with an isolate scope:
.directive('wrapper', function() {
var link = function(scope, element, attrs, ngModel) {
scope.$watch(attrs['ngModel'], function() {
var model = ngModel.$modelValue;
console.log("----- Wrapper model updated", model);
scope.model = model;
})
};
return {
restrict: 'E',
require: 'ngModel',
link: link,
scope: {
},
template: "<div><h2>Echo:</h2> <echo id='myEcho' ng-model='model'></echo></div><div><h2>Model text:</h2>{{ model.text }}</div>"
}
})
You can see that the "wrapper" directive requires an ngModel
, as does the echo
directive.
When I use the "wrapper" directive in my HTML, and then I push a value into the ngModel, the "wrapper" directive correctly identifies that the model has changed (the console.log shows this). At that point, the wrapper directive then updates the model on its scope, which I would have expected would propagate that model update into the "echo" directive.
However, watching the console, the "echo" directive never sees the model get updated.
Q: Why doesn't the "echo" directive see the updated model from the "wrapper" directive?
Note: This may be made slightly more complex by the fact that the "echo" is not only consumed from the "wrapper" directive - sometimes it is consumed directly.