0
我有一个链接到窗体上的文本框的指令,我希望这个指令设置'required'错误。如何在指令窗体中的元素上设置有效值?
这里有一个小提琴,显示什么,我试图做
http://jsfiddle.net/scottieslg/7qLsj3rr/3/
HTML:
<div ng-app='myApp' ng-controller='TestCtrl'>
<ng-form name='testForm'>
<input type='text' name='myInput' />
<div ng-messages="testForm.myInput.$error">
<div ng-message="required">Required</div>
</div>
<test-directive ng-model='testModel'></test-directive>
</ng-form>
</div>
的Javascript:
var app = angular.module('myApp', ['ngMessages']);
app.controller('TestCtrl', function($scope) {
$scope.testModel = {}
});
app.directive('testDirective', function() {
return {
restrict: 'E',
require: 'ngModel',
template: '<div><button ng-click="setError()">Set Error</button></div>',
link: function(scope, element, attrs, ngModelCtrl) {
scope.setError = function() {
// How can I set .setValidate('require', true) on myInput from here??
}
}
}
});
为什么你需要'test-directory'的'ng-model'? – 2015-03-08 19:09:10
从你的问题中不清楚你想要做什么,请更新你的问题 – 2015-03-08 19:35:04
当字段有值时,你不能假装'required'验证器应该失败。我认为你可能有兴趣创建你的[自定义验证逻辑](https://docs.angularjs.org/guide/forms#custom-validation)。 – fracz 2015-03-08 19:46:56