如何使用ng-model名称访问ng-minlength和ng-maxlength

时间:2018-07-18 05:33:28

标签: html angularjs

我需要使用ng-model访问/使用min和max,因为我没有表单名称和输入字段名称来访问错误/有效范围。

<input type="text" ng-minlength="10"  ng-maxlength="30"  ng-model="value.x" >
    <p ng-if="value.x.$error" class="help-block">Please enter above {{minlength}} characters</p>
    <p ng-if="value.x.$error" class="help-block">Please enter below {{maxlength}} characters</p>

2 个答案:

答案 0 :(得分:0)

使用value.x

的长度
<input type="text" ng-minlength="10"  ng-maxlength="30"  ng-model="value.x" >
    <p ng-if="value.x.length < 10" class="help-block">Please enter above {{minlength}} characters</p>
    <p ng-if="value.x.length > 30" class="help-block">Please enter below {{maxlength}} characters</p>

答案 1 :(得分:0)

我猜这是ng-minlength和ng-maxlength的问题。他们没有在我的工作。请尝试下面对我有用的代码。

<input type="text" ng-model="value.x" />
<p ng-if="value.x.length < minlength" >Please enter above {{minlength}} characters</p>
<p ng-if="value.x.length > maxlength" >Please enter below {{maxlength}} characters</p>

Angularjs

$scope.value = {x:10};
$scope.minlength = 5;
$scope.maxlength = 10;

并检查以下问题:https://github.com/angular/angular.js/issues/3669