Reputation: 7204
I have problem with ngModel.NgModelController.$parsers. I explain it at example: I create directive for input field. Each time I change value in this input in html then it should call my registered function.
And this registered function should always change what you write in input field to 'A' char, but modify model.
html file:
<div ng-app="myApp">
<form>
<input ng-model="myval1" somedir/>
</form>
</div>
and js file:
var myApp = angular.module('myApp', []);
myApp.directive("somedir", function(){
return {
restrict:'A',
require: '?ngModel',
link: function(scope, element, attrs, ngModelController) {
ngModelController.$parsers.unshift(function(val){
console.log("i'm here");
ngModelController.$viewValue ="A";
ngModelController.$render();
return val;
});
}
}
});
I prepare jsfiddle to tests:
angular 1.2.9 : http://jsfiddle.net/hwzxfon3/1/
angular 1.4.7 : http://jsfiddle.net/372re41m/1/
And in angular 1.2.9 it work correct. In angular 1.4.7 there is some problem. How to get it:
focus on input field, and press 3 times same button e.g. '1'.
First time it show 'A' in input field and in browser console i'm here
. Second time also the same. But third time it show 'A'+pushed char (e.g. 'A1') and not write in browser console i'm here
which means that registered function was not called.
Please tell me if, I have bug in my code (please write what to change), or it is bug in angular (then i report bug to angular team)? I test it in latest chrome and safari in os x (10.10.5). If it works for you without problem please write this too.
Upvotes: 1
Views: 257
Reputation: 7204
I report this problem to angular team. Full response is there https://github.com/angular/angular.js/issues/13068.
In short, in similar situation it is better to use this:
ngModelController.$setViewValue("A");
then this:
ngModelController.$viewValue ="A";
Working jsfiddle for angular 1.4.7: http://jsfiddle.net/455qdd6p/1/
I needed it to solve for my angular module https://github.com/uhlryk/angular-dynamic-number
Upvotes: 1