Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Using angular 1.3.x, I have a parser in a directive assigned to an input.

When the user enters text into the directive the function that has been pushed to the $parses array is run. However when ngModel.$setViewValue(res); is called the same parse function is immediately called which didn't happen in angular 1.2.x

ngModel.$parsers.push(function (inputValue) {
    var originalVal = element.val();

    // do some logic to originalVal and set it as new ViewValue if changed

    if (originalVal != inputValue ) {
        ngModel.$setViewValue(res);
        ngModel.$render();
    }
});

Any ideas?

share|improve this question
    
What are you trying to do exactly? –  Blackhole yesterday
    
@Blackhole basically as the user types a thousand seperator number commas will be added to the text –  SuperUberDuper yesterday
    
Isn't that the use case for a $formatter instead of a $parser? –  JoseM 6 hours ago
    
@JoseM I saw it done with $parser here: jsfiddle.net/dubilla/dj6mX/798 but doesn't work with angular 1.3 –  SuperUberDuper 5 hours ago

1 Answer 1

up vote 1 down vote accepted

The $parsers documentation states

Array of functions to execute, as a pipeline, whenever the control reads value from the DOM. Each function is called, in turn, passing the value through to the next. The last return value is used to populate the model. Used to sanitize / convert the value as well as validation. For validation, the parsers should update the validity state using $setValidity(), and return undefined for invalid values.

The emphasis is mine. So you need to return a value.

ngModel.$parsers.push(function (inputValue) {
    var originalVal = element.val();

    // do some logic to originalVal and set it as new ViewValue if changed

    if (originalVal != inputValue ) {
        ngModel.$setViewValue(res);
        ngModel.$render();
    }

    // make sure to return the updated view value
    return res;
});

I updated the sample fiddle to show that it works http://jsfiddle.net/dj6mX/1138/

share|improve this answer
    
thanks man! I am getting tired of angular and moving to ember and polymer –  SuperUberDuper 3 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.