我在 a 中有一个输入字段ng-repeat,它获取一个动态名称标签,如下所示:
<div ng-repeat="a in b track by $index">
<div show-errors >
<label for="new_taxid_abroad_{{$index}}"> Bla </label>
<input name="new_taxid_abroad_{{$index}}" ng-model="xxx" type="text">
</div>
</div>
show-errors 验证器如下所示:
.directive('showErrors', function($timeout) {
var validate = function($element, valid) {
$element.toggleClass('has-error', !valid);
$element.toggleClass('has-success', valid);
};
return {
restrict: 'A',
require: '^form',
controller: function($scope, $element, $attrs) {
this.$validate = function(name) {
validate($element, name);
};
},
link: function($scope, $element, $attrs, formCtrl) {
// find the text box element, which has the 'name' attribute
var inputEl = $element[0].querySelector("[name]");
// convert the native text box element to an angular element
var inputNgEl = angular.element(inputEl);
// get the name on the text box so we know the property to check
// on the form controller
var inputName = inputNgEl.attr('name');
var validateEvent = function(evt, name) {
if (name && name != inputName)
return;
$timeout(function() {
validate($element, formCtrl[inputName] ? formCtrl[inputName].$valid : false);
}, 0);
};
// only apply the has-error class after the user leaves the text box
inputNgEl.bind('blur', validateEvent);
$scope.$on('show-errors-check-validity', validateEvent);
}
}
});
我正在尝试使用name tag. 到目前为止,该{{index}}部分不会被渲染,因此验证器不起作用。
根据 Github,能够做到这一点是在 1.3 之后修复的,我正在运行 1.5,但到目前为止还没有运气!
有小费吗?