gpt4 book ai didi

angularjs - 如何在AngularJS中做条件最大长度

转载 作者:行者123 更新时间:2023-12-03 06:56:54 24 4
gpt4 key购买 nike

有谁知道如何在 angularjs 中执行条件 maxlength,我发现需要条件但不是 maxlength 等....

例如,如果给定 maxlength ...它应该验证,但如果 maxlength 为 0 或 false,则不应该......

$scope.validation = {a: {required:true,minlength:5, maxlength:10}, 
b: {required:false,minlength:0, maxlength:false}}

使用 ng-repeat 循环创建 Html...现在的问题是 maxlength 每次都会被触发...而我已在第二个输入字段中禁用它

<input type="text" name="myName" ng-model="name" ng-minlength='validation.a.minlength' ng-maxlength="validation.a.maxlength" ng-required='validation.a.required'  />


<input type="text" name="myName1" ng-model="name1" ng-minlength='validation.b.minlength' ng-maxlength="validation.b.maxlength" ng-required='validation.b.required' />

最佳答案

如果您通过 Angular 表达式使用空字符串作为“false”,这些参数似乎有效。

<input type="text" name="myName" ng-model="name" ng-minlength='{{validation.a.minlength}}' ng-maxlength="{{validation.a.maxlength}}" ng-required='validation.a.required'  />
<input type="text" name="myName1" ng-model="name1" ng-minlength='{{validation.b.minlength}}' ng-maxlength="{{validation.b.maxlength}}" ng-required='validation.b.required' />

$scope.validation = {
a: {required:true, minlength:5, maxlength:10},
b: {required:false, minlength:'', maxlength:''}
}

<罢工> plunker

plunker对于1.3.2

现在可以使用 0 和 Number.MAX_SAFE_INTEGER忽略这个规则。或者也许只需要 999 就足够了:)

 b: {required:false, minlength:0, maxlength:Number.MAX_SAFE_INTEGER}

关于angularjs - 如何在AngularJS中做条件最大长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24932516/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com