我已经创建了一个用于重复删除表单错误消息代码的指令:
angular.module('errorMessages', ['ngMessages'])
.directive('meErrorMessages', function() {
return {
templateUrl: '/app/common/errorMessages/errorMessages.html',
transclude: true,
scope : {
formName: '=',
inputName: '='
},
link: function ($scope, $element, $attrs, $ctrl, $transclude) {
$transclude(function(
angular.forEach(clone, function(cloneElement) {
cloneElement.class = 'errorBox';
})
})
}
}
});
使用模板HTML:
<div ng-show="inputName.$touched || formName.$submitted" ng-messages="inputName.$error">
<ng-transclude></ng-transclude>
</div>
但是,在HTML中使用模板功能时:
<input id="name" name="name" type="text" ng-model="user.name" required />
<me-error-messages form-name="registrationForm"
input-name="registrationForm.name">
<div ng-message="required">Required</div>
</me-error-messages>
我得到了
Error: [$compile:ctreq] Controller 'ngMessages', required by directive 'ngMessage', can't be found!
当我删除link
功能时,会正确显示和隐藏已转换的内容。 $transclude
函数有什么问题?如何修改已转换内容的DOM?