Angular.js $ setValidity onLoad Page

2
  • Scenery:

    I have a form with 3 steps, a jsp for each step. In a given jsp there is a input text with the directive ng-minlength and a onChange that calls a function that validates with $setValidity if the value entered is invalid.

  • Problem:

    When the field is invalid and navigation between the steps occurs, returning to step the field loses the status of invalid. As a workaround, I tried to validate the field when loading the page but there is an error executing the code below because the form has not yet been loaded:

    $scope.form.numeroCartao.$setValidity("valid", true);

  • Question:

    How to validate a field with $setValidity when loading page?

asked by anonymous 05.05.2015 / 15:27

1 answer

2

Instead of setting the value of the field using the value of the input, load the value in ng-model .

Instead of doing this:

<input type="text" ng-minlenth="10" ng-model="teste" value="${ vdo.teste }"/>

Do this:

<form ng-init="teste = '${ vdo.teste }';">
...
<input type="text" ng-minlenth="10" ng-model="teste"/>

Another ... do not listen to the onChange native event of the element, with Angular ideally using the on-change directive, or creating a $ watch on your controller.

    
08.05.2015 / 17:13