我只想使用ng-init设置一个minlength变量,然后在两个地方重复使用它.
原文链接:https://www.f2er.com/angularjs/141145.htmlangular.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>