<form ng-app="form-example" name="form" class="row form-horizontal" novalidate>
<div class="control-group">
<label class="control-label" for="inputEmail">Email || {{form.email.$error}}</label>
<div class="controls">
<input type="email" name="email" id="inputEmail" placeholder="Email" ng-model="email" password-validate required>
<div class="input-help">
<h4>Invalid Email</h4>
</div>
</div>
</div>
</form>
var app = angular.module('form-example', []);
app.directive('passwordValidate', function() {
return {
restrict: 'AE',
require : 'ngModel',
link: function(scope, element) {
var modelController = element.find('input').controller('ngModel');
modelController.$setValidity('myrequired', true);
}
}
});
见小提琴
http://jsfiddle.net/pmKpG/402/
我正在尝试将我的自定义有效性从指令推送到视图,但它不会呈现输出请指导
我可以在html中看到{{form.email。$ error}}没有将myrequired对象显示为true
答案 0 :(得分:0)
您可以像这样访问ngModel控制器,因为您设置了require: 'ngModel'
:
app.directive('passwordValidate', function() {
return {
restrict: 'AE',
require : 'ngModel',
controller: function(scope, ngModelCtrl) {
ngModelCtrl.$setValidity('myrequired', true);
},
link: function(scope, element) {
}
}
});