How to get the value of the validation error (= entered value, but not in the model) Angular way

I'm trying to get the value of user.name.length in the error message. If jQuery is used it is doable, but isn't there a method unique to Angularjs?

<form novalidate name="myForm" ng-submit="addUser()">
<p>Name:
<input type="text" name="name" ng-model="user.name" required ng-minlength="5" ng-maxlength="8">
<span ng-show="myForm.name.$error.minlength">{{user.name.length}} too short!</span>

      

+3


source to share


2 answers


Can you try with {{myForm.name.$viewValue.length}}

as below:



<input type="text" name="name" ng-model="user.name" ng-minlength="5" ng-maxlength="8" />
                <span ng-show="myForm.name.$error.minlength">{{myForm.name.$viewValue.length}} too short!</span>
                <span ng-show="myForm.name.$error.maxlength">{{myForm.name.$viewValue.length}} too long!</span>

      

+6


source


How ng-minlength and ng-maxlength work, if the input textbox has fewer or more characters than the two directives specified, then the user.name is blank (I don't even think it is defined). To see what I mean, add {{user.name}}

after the <span>.



0


source







All Articles