Is there a way to insert a minlength / maxlength value in a message in ng messages?

Instead of just telling them: "Your field is too short" to tell the user: "Your field must be at least {{ value }} characters" instead. Can you do this in ng messages?

+6
source share
1 answer

I would just use ng-init to set the minlength variable and then reuse it in both places.

 angular.module('myApp', ['ngMessages']); 
 <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular-messages.js"></script> <form name="form" ng-app="myApp"> <div ng-init="minlength = 5"> <input type="text" name="name" ng-model="name" ng-minlength="minlength" > <div ng-messages="form.name.$error"> <div ng-message="minlength"> Your field must be at least {{minlength}} characters long. Current length: {{form.name.$viewValue.length}} </div> </div> </div> </form> 
0
source

Source: https://habr.com/ru/post/983063/


All Articles