angularjs – 有没有办法在ng-messages中将minlength / maxlength的值插入到消息中?

前端之家收集整理的这篇文章主要介绍了angularjs – 有没有办法在ng-messages中将minlength / maxlength的值插入到消息中?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想,而不只是告诉用户“你的字段太短”,告诉用户,“你的字段必须至少{{value}}字符长”.你能用ng-messages做到这一点吗?
我只想使用ng-init设置一个minlength变量,然后在两个地方重复使用它.
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>
原文链接:https://www.f2er.com/angularjs/141145.html

猜你在找的Angularjs相关文章