admin管理员组文章数量:1193808
I writing a web app in angularjs and using angular material (not sure if that is relevant to the question) and ngMessages to provide feedback for invalid inputs to the user.
Usually, for validation of directives provided, I can validate in a way similar to:
<md-input-container>
<input required name="myInput" ng-model="someModel">
<div ng-messages="formName.myInput.$error">
<div ng-message="required">This field is required.</div>
</div>
</md-input-container>
But if I created my own custom directive...
moduleName.directive('ngCustomdir', function(){
return {
restrict: 'A',
require: 'ngModel',
link: function($scope, $element, $attrs, ngModel){
//do some validation
return validation; //<--true or false based on validation
}
};
}
and include it in my input...
<input required ng-customdir name="myInput" ng-model="someModel">
I know it validates the input, because if the input is invalid based on my custom directive's validation, the field turns red and the $invalid value is set to true, but I do not know how to make a message appear with ngMessages based on when this happens.
<div ng-messages="formName.myInput.$error">
<div ng-message="required">This field is required.</div>
<div ng-message="customdir">This is what I need to appear.</div>
</div>
I cannot seem to get a message to appear when my custom directive validation is invalid. How do I do this? Thanks for the help in advance.
I writing a web app in angularjs and using angular material (not sure if that is relevant to the question) and ngMessages to provide feedback for invalid inputs to the user.
Usually, for validation of directives provided, I can validate in a way similar to:
<md-input-container>
<input required name="myInput" ng-model="someModel">
<div ng-messages="formName.myInput.$error">
<div ng-message="required">This field is required.</div>
</div>
</md-input-container>
But if I created my own custom directive...
moduleName.directive('ngCustomdir', function(){
return {
restrict: 'A',
require: 'ngModel',
link: function($scope, $element, $attrs, ngModel){
//do some validation
return validation; //<--true or false based on validation
}
};
}
and include it in my input...
<input required ng-customdir name="myInput" ng-model="someModel">
I know it validates the input, because if the input is invalid based on my custom directive's validation, the field turns red and the $invalid value is set to true, but I do not know how to make a message appear with ngMessages based on when this happens.
<div ng-messages="formName.myInput.$error">
<div ng-message="required">This field is required.</div>
<div ng-message="customdir">This is what I need to appear.</div>
</div>
I cannot seem to get a message to appear when my custom directive validation is invalid. How do I do this? Thanks for the help in advance.
Share Improve this question asked Jun 30, 2015 at 0:11 northsideknightnorthsideknight 1,5571 gold badge15 silver badges31 bronze badges2 Answers
Reset to default 21You are getting very close, just add some validation in your link
function:
link: function($scope, $element, $attrs, ngModel){
ngModel.$validators.required = function(modelValue) {
//true or false based on required validation
};
ngModel.$validators.customdir= function(modelValue) {
//true or false based on custome dir validation
};
}
When the model changes, AngularJS will call all the validate functions in $validators
object. The ng-message will display based on the function name.
More infomation about the $validators:
https://docs.angularjs.org/api/ng/type/ngModel.NgModelController
http://blog.thoughtram.io/angularjs/2015/01/11/exploring-angular-1.3-validators-pipeline.html
There is more compact alternative to Huy's solution:
link: function($scope, $element, $attrs, ngModel){
//validation true or false
ngModel.$setValidity('required',validation);
ngModel.$setValidity('customdir',validation);
}
本文标签: javascriptValidate custom directive using ngMessagesStack Overflow
版权声明:本文标题:javascript - Validate custom directive using ngMessages - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738491722a2089745.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论