lundi 30 mars 2015

Angular Form validation using ControllerAs

I am currently facing the following problem:


I would like to validate my form input using the Angular ng-model directives, when using those together with $scope they work fine.


Now working with the Controller As syntax they fail to work.


This problem is poorly documented, the only help I could find is: http://ift.tt/1qAjNoS


Here is a small example of my code:


The html template gets called with Controller as vm



<form name="vm.signUpForm" ng-submit="vm.signup(vm.user)">

<label for="name">Name</label>

<input type="text"
class="form-control"
id="name"
name="name"
placeholder="Full name"
ng-model="vm.user.name"
ng-minlength="2" required />

<div ng-show="vm.signUpForm.$submitted || vm.signUpForm.name.$touched">
<span ng-show="vm.signUpForm.name.$error.required">Please fill in your name</span>
<span ng-show="vm.signUpForm.name.$error.minlength">A minimum of 2 characters is required</span>
</div>
...
</form>


Am I forced to use $scope for angular form validation ? Or did I miss something ?


Thanks in advance


Aucun commentaire:

Enregistrer un commentaire