我目前正在学习AngularJS,并且正在尝试为表单上的输入字段创建自定义指令。该指令将根据您提供的规则具有动态验证规则。我遇到的问题是formController和ngMOdelController的$ dirty& amp; $ pristine属性即使在与场地交互后也不会改变。我有一个插件设置来说明我的问题:http://plnkr.co/edit/JahbBHZm7sH8t6qWxOBY
以下是片段:
app.directive('myDirective', ['$timeout', '$compile', function($timeout, $compile) {
return {
restrict: 'A',
require: ['?^form', '?ngModel'],
templateUrl: 'field-template.html',
scope: {
value: '=ngModel',
fieldName: '@',
validation: '@',
labelText: '@'
},
link: function(scope, element, attributes, controllers) {
var $element = $(element);
scope.form = controllers[0];
scope.model = controllers[1];
$timeout(function() {
if (!scope.validation) return;
var validation = scope.$eval(scope.validation);
for (var key in validation) {
$element.find('#' + scope.fieldName).attr(key, validation[key]);
}
$compile($element.contents())(scope);
});
}
};
}]);