Reputation: 15637
I'm creating a custom input directive. My goal is achieve that form.FormController
behave like any other <input>
elements with ng-require="true"
.directive('myinput', function($window, $location, $sce, $dropdown) {
return {
restrict: 'A',
require: '^form',
scope: {
model: '=myModel'
},
link: function postLink(scope, element, attr, formCtrl) {
scope.$watch('model', function(newValue, oldValue) {
if (newValue) {
formCtrl.$setValidity(attr.myModel, true);
} else {
formCtrl.$setValidity(attr.myModel, false);
}
}, true);
}
}
The above sample works fine form[myModel].$valid
becomes true or false depending on myModel value. But that i want is form[myModel].$error.required
becomes true.
As I can achieve this behavior?
Upvotes: 0
Views: 413
Reputation: 1887
The first parameter for $setValidity method is the validator token, so what you can do is this:
/*
Point first to your input field -> formCtrl[attr.myModel]
and after that set the type of validator token: 'required' in your example
*/
formCtrl[attr.myModel].$setValidity('required', true);
Check this example which use the build-in required directive and the custom one:
http://jsfiddle.net/oua78v19/10/
Upvotes: 1