Let's say I have the following directive:
myApp.directive('myDirective', function() { return { restrict: 'A', require: 'ngModel', scope: { ngModel: '=' }, link: function(scope, elem, attrs, ngModelCtrl) { scope.$watch('ngModel', function() { ngModelCtrl.$modelValue = 'foo'; }); } } });
And the following html:
<input ng-model="name" my-directive></input>
Basically, whenever a user changes input, my-directive ideally changes the value of the internal model to "foo", leaving the view value intact.
But when I print $scope.name in the corresponding controller, it does not write "foo", it logs everything that the user entered.
It would seem that ngModelCtrl.$modelValue not what the controller is accessing - am I really getting it wrong?
(Also looking at ngModel in the area is really wrong, but I'm not sure of anything else. Any suggestions would be greatly appreciated!)
angularjs angular-ngmodel angularjs-directive
Cody
source share