使用插值在输入上动态生成验证属性

2024-01-26

编辑:这个问题从 Angular 开始不再相关version: 1.3.0-beta.12您现在可以解析 ng-minlength 和 ng-maxlength 动态值。看:https://github.com/angular/angular.js/issues/5319 https://github.com/angular/angular.js/issues/5319

我的问题很简单:我需要动态创建输入验证(例如ng-minlength)使用插值法。

这样做我遇到了一些问题,专门为以下内容生成验证属性:ng-minlength and ng-maxlength。我认为这是因为他们只采用常数?

下面你可以看到我的代码,我通过outerForm使用包装器的原因是我无法使用插值动态生成输入元素的名称属性,并且我必须将每组重复输入包装在一个ngForm directive并将它们嵌套在外部表单元素中。

再说一遍,问题出在属性上ng-minlength="field.ValidationAttributes['data-val-length-min']"未正确设置。

当我直接使用打印值时{{field.ValidationAttributes['data-val-length-min']}}该值显示正确。

我是否必须创建一个指令来解析我的信息,是否需要创建自己的最小/最大验证,或者我只是遇到语法错误?

<form name="outerForm">
   <div ng-repeat="field in logEntry.StringValues">
      <ng-form name="innerForm">
         <input type="text" name="foo" ng-model="item.foo" ng-minlength="field.ValidationAttributes['data-val-length-min']" required/>
         <span ng-show="innerForm.foo.$error.required">required</span>
         <span ng-show="innerForm.foo.$error.minlength">to short</span>
      </ng-form>
   </div>
</form>

您好,您可以使用 double {} 来插入动态验证规则,请参阅此处:http://jsbin.com/xayiro/1/ http://jsbin.com/xayiro/1/edit

如果您可以发布您的 field.Validation Attributes 模型,我可以更新 jsbin。

HTML:

 <ng-form name="innerForm">
     <input type="text" name="foo" ng-model="item.foo" ng-minlength="{{validation.minlength}}" required/>
     <span ng-show="innerForm.foo.$error.required">required</span>
     <span ng-show="innerForm.foo.$error.minlength">to short</span>
  </ng-form>

JS:

 $scope.validation= {

    maxlength:20,
    minlength:3
  };
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用插值在输入上动态生成验证属性 的相关文章

随机推荐