AngularJS过滤器不适用于所需的输入字段

时间:2014-02-09 18:28:31

标签: forms angularjs field

这项工作

<input type="text" aaa="{{field.isRequired | requiredx}}" ng-model="field.input.model">

输出

<input type="text" aaa="required" class="form-control ng-pristine ng-valid" ng-model="field.input.model">

但这不起作用

<input type="text" required="{{field.isRequired | requiredx}}" ng-model="field.input.model">

输出

<input type="text" required="{{field.isRequired | requiredx}}" class="form-control ng-pristine ng-valid ng-valid-required" ng-model="field.input.model">

...

编辑,添加过滤器:

angular.module('appFilters', []).filter('requiredx', function() {
return function(input) {
    return input ? 'required' : '';
};

}); 任何人都知道如何使它工作?

1 个答案:

答案 0 :(得分:1)

使用ng-required,您无法设置HTML5所需的属性。试试这个:

<input type="text" ng-required="(field.isRequired | requiredx)" ng-model="field.input.model">