sne11ius sne11ius - 2 years ago 117
AngularJS Question

Get "raw" value from invalid input field

I have an input field in a form with some validations. It works like a charm.

It basically looks like this:


A working plunkr is here:

But now I also want to react to every input change - whether valid or not. So for example if the input field contains "123" it is not valid and the value is not transferred to my model - thats fine. But I still want to get the value to do some intermediate requests to a webservice.

Any Ideas?

Answer Source

Here is what i came up with for your scenario.

Basically you can write a directive which requires ngModel (ngModelController). The ngModelController has a array of parsers which it call to parse the view value in a pipeline manner. If validation fail these parsers do not update the model. If you inject a custom parser at the start of this parsers array, you can get the each view change value and do anything you want with it.

See my plunkr here (See console)

The basic code would be

ngModelCtrl.$parsers.splice(0,0,(function (viewValue) {
              console.log("The view value is:"+viewValue)
                return viewValue;           

Also see ngModelController documenation

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download