如何重置角度js中的自定义验证 [英] how to reset the custom validations in angular js

查看:86
本文介绍了如何重置角度js中的自定义验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ $ scope.formname setpristine();重置所有预定义的验证(例如:必需)但自定义验证消息未重置为其原始状态



示例代码:以下代码工作正常意味着显示错误消息但在重置表单时,验证未被清除



 <  < span class =code-leadattribute> input     type     =  text    ng-model   =  studentobj.firstname    必需    checkexists     /  >  
< span style = color:red ng-show = testform。$ error.required&& testform。$ dirty&& testform。$ invalid > 名字必需< / span >
< span style = color:red ng- show = testform。$ error.checkexists > 匹配的名称< / span >







 app.directive ('  checkexists',function(){

返回 {
restrict:' A'
require:' ngModel'
link:function(scope,element,attrs,ngctrl )
{


ngctrl。$ validators.checkexists = function(ngctrl)
{
if (ngctrl == phani
{
return true ;
console.log(' if');

}
else {
console.log(' else'
return false < /跨度>;
}


}

}

}

解决方案

setpristine();重置所有预定义的验证(例如:必需)但自定义验证消息未重置为其原始状态



示例代码:以下代码工作正常意味着显示错误消息但在重置表单时,验证未被清除



 <  < span class =code-leadattribute> input     type     =  text    ng-model   =  studentobj.firstname    必需    checkexists     /  >  
< span style = color:red ng-show = testform。


error.required&& testform。


dirty&& TESTFORM。

$scope.formname.$setpristine(); resetting all the predefined validations(ex:required) but custom validation messages are not resetting to its original state

example code: The below code is working correctly means shows error messages but while resetting the form the validations not cleared

<input type ="text" ng-model="studentobj.firstname" required checkexists />
<span style="color:red" ng-show="testform.$error.required && testform.$dirty && testform.$invalid">first name Required</span>
<span style="color:red" ng-show="testform.$error.checkexists ">Name Matched</span>




app.directive('checkexists',function(){

    return{
        restrict:'A',
        require:'ngModel',
        link:function(scope,element,attrs,ngctrl)
        {


             ngctrl.$validators.checkexists =function(ngctrl)
             {
                 if(ngctrl=="phani")
                 {
                     return true;
                     console.log('if');

                 }
else{
    console.log('else')
    return false;
}


             }

        }

    }

解决方案

setpristine(); resetting all the predefined validations(ex:required) but custom validation messages are not resetting to its original state

example code: The below code is working correctly means shows error messages but while resetting the form the validations not cleared

<input type ="text" ng-model="studentobj.firstname" required checkexists />
<span style="color:red" ng-show="testform.


error.required && testform.


dirty && testform.


这篇关于如何重置角度js中的自定义验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆