I'm new to Angular and I want to check I am doing form validation right. I want to display an appropriate message when the user has entered more than 100 characters in an input field.
For this I am using $viewValue. Would $modelValue be better for validation since it is what has been 'saved' in the controller? Is there a better way to do this kind of specific validation?
<label for="Forename" class="control-label col-md-2">Forename</label>
<input id="forename" type="text" class="form-control"
Forename must be filled in
Forename was not valid
<li ng-show="detailsForm.detailsForename.$viewValue.length > 100">
Forename must be no more than 100 characters
As noted in the ngMaxlength docs: "The validator sets the maxlength error key if the ngModel.$viewValue is longer than the integer ..."
So in your case that would be:
You should never need to access an
$modelValue from the template.