Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
20
rated 0 times [  27] [ 7]  / answers: 1 / hits: 21790  / 11 Years ago, sun, september 22, 2013, 12:00:00

I have searched everywhere for this. Every stack overflow that has an answer, it does not actually work. Same with any examples or google group examples from for angular including the docs.



Seems simple. I want a function to get called on an input for each key pressed by the user.



simple input with a ng-model



<input class=form-control ng-model=model.thisisnotfun formatter type=text required>


According to everything i read. $formatters should update the value from the model to the view calling any functions in the $formatters array. They never get called when i type in the input box.



.directive('formatter', function ($filter, $parse) {
return {
require: 'ngModel',

link: function (scope, element, attrs, ngModel) {

ngModel.$formatters.push(
function (value) {
console.log('this only gets called on page load');
}
);
}
};
})


I know there are plenty of custom ways to do this, which i have already done. I do not need a work around, i just want to know how to use $formatters correctly to format the view data.



very very simple jsfiddle http://jsfiddle.net/fh7sB/4/



Thanks for any help.


More From » angularjs

 Answers
3

The $formatters get called when the model is updated in order to format the value to be presented to the user. The $parsers do the opposite, i.e. translate the value from a view string to a real model, e.g. a number.



In the forked fiddle here: http://jsfiddle.net/9tuCz/ press the button; it changes the model and fires the $formatters again.


[#75529] Friday, September 20, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
shelbiec

Total Points: 101
Total Questions: 106
Total Answers: 106

Location: Ivory Coast
Member since Fri, Oct 8, 2021
3 Years ago
;