Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get "raw" value from invalid input field

Tags:

angularjs

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

It basically looks like this:

<input
  class="form-control"
  type="number"
  ng-model="zipcode"
  ng-minlength="5"
  ng-maxlength="5"
  id="zipcode"
  name="zipcode"
  required
>

A working plunkr is here: http://plnkr.co/edit/H0h59kG75T5MGE9cAhSo?p=preview

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?

like image 572
sne11ius Avatar asked Nov 06 '13 16:11

sne11ius


2 Answers

First call the form element in your controller, then use the $viewValue attribute :

View :

<form name="form">
<input
  ...
  ng-model="zipcode"
  ng-change="getRawValue(form)"
  name="zipcode"
  required
>
</form>

Controller:

$scope.getRawValue = function(form) {
  var rawValue = form.zipcode.$viewValue;
}
like image 82
Deniz Avatar answered Nov 15 '22 05:11

Deniz


Angular 1.3 introduced a real answer for this: allowInvalid in ngModelOptions.

Example:

<input 
    type="text"
    name="userName"
    ng-model="user.name"
    ng-model-options="{allowInvalid: true}"
>
like image 12
metamatt Avatar answered Nov 15 '22 07:11

metamatt