Html $dirty&&$原始的不能正常工作
我试图编写一个代码,只有当输入类型中的文本更改或清除时,控件才会进入代码块。我正在使用以下条件:Html $dirty&&$原始的不能正常工作,html,angularjs,input,Html,Angularjs,Input,我试图编写一个代码,只有当输入类型中的文本更改或清除时,控件才会进入代码块。我正在使用以下条件: if(myfrm.textchecker.$dirty && !myfrm.textchecker.$pristine) 直到我将其更改为: if(!myfrm.textchecker.$dirty && !myfrm.textchecker.$pristine)//completely opposite 以下是我的代码供参考: <html> &
if(myfrm.textchecker.$dirty && !myfrm.textchecker.$pristine)
直到我将其更改为:
if(!myfrm.textchecker.$dirty && !myfrm.textchecker.$pristine)//completely opposite
以下是我的代码供参考:
<html>
<head>
<script src="angular.min.js"></script>
</head>
<body ng-app="myApp">
<div ng-controller="outerCtrl">
<form name="myfrm">
<input type="password" name="textchecker" ng-change="processed(ngdata)" ng-model="ngdata" required/>
<input type="submit" name="submit" value="submit">
</form>
<span style="color:red" ng-show="myfrm.textchecker.$error.required">please enter the required text</span>
{{ngdata}}
<p>
{{final}}
</p>
<p>
$dirty : {{ myfrm.textchecker.$dirty }} </br>
$invalid : {{myfrm.textchecker.$invalid }}
</br>
$pristine : {{myfrm.textchecker.$pristine }}
</p>
</div>
</body>
</html>
<script>
var app=angular.module("myApp",[]);
app.controller("outerCtrl",function($scope){
$scope.processed=function(password)
{
if(!myfrm.textchecker.$dirty && !myfrm.textchecker.$pristine)
{
console.log('!myfrm.textchecker.$dirty + !myfrm.textchecker.$pristine');
console.log(!myfrm.textchecker.$dirty + !myfrm.textchecker.$pristine);
// var password=$scope.ngdata;
var strength=0;
//console.log(password);
// alert($scope.ngdata);
// if (password.length > 7) strength += 1
//if password contains both lower and uppercase characters, increase strength value
if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) strength += 1
//if it has numbers and characters, increase strength value
if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)) strength += 2
//if it has one special character, increase strength value
if (password.match(/([!,%,&,@,#,$,^,*,?,_,~])/)) strength += 3
//if it has two special characters, increase strength value
if (password.match(/(.*[!,%,&,@,#,$,^,*,?,_,~].*[!,%,&,@,#,$,^,*,?,_,~])/)) strength +=5
//now we ha
if(strength<=2)
{
$scope.final="Poor";
}
else
if(strength>2 && strength<=5)
{
$scope.final="Weak";
}
else
if(strength>5 && strength<=9)
{
$scope.final="Good";
}
if(strength>9)
{
$scope.final="Strong";
}
}
}
});
</script>
请输入所需的文本
{{ngdata}}
{{final}}
$dirty:{{myfrm.textchecker.$dirty}}
$invalid:{myfrm.textchecker.$invalid}
$pristine:{myfrm.textchecker.$pristine}
var-app=angular.module(“myApp”,[]);
应用控制器(“outerCtrl”,功能($scope){
$scope.processed=函数(密码)
{
如果(!myfrm.textchecker.$dirty&&!myfrm.textchecker.$pristine)
{
console.log(“!myfrm.textchecker.$dirty+!myfrm.textchecker.$pristine”);
console.log(!myfrm.textchecker.$dirty+!myfrm.textchecker.$pristine);
//var password=$scope.ngdata;
var强度=0;
//console.log(密码);
//警报($scope.ngdata);
//如果(password.length>7)强度+=1
//如果密码同时包含小写和大写字符,请增加强度值
如果(密码匹配(/([a-z].[a-z])|([a-z].[a-z])/)强度+=1
//如果有数字和字符,则增加“强度”值
if(password.match(/([a-zA-Z])/)和&password.match(/([0-9])/)强度+=2
//如果它有一个特殊字符,增加强度值
如果(密码匹配(/([!,%,&,@,#,$,^,*,?,,~])/)强度+=3
//如果有两个特殊字符,则增加“强度”值
如果(password.match(/(.[!,%,&,@,#,$,^,*,?,,~].[!,%,&,@,#,$,^,*,?,,~])/)强度+=5
//现在我们
if(强度2和强度5和强度9)
{
$scope.final=“Strong”;
}
}
}
});
要在控制器中使用窗体的$dirty&$pristine
,必须使用$scope
访问它们
if ($scope.myfrm.textchecker.$dirty && !$scope.myfrm.textchecker.$pristine) {
var-app=angular.module(“myApp”,[]);
应用控制器(“outerCtrl”,功能($scope){
$scope.processed=函数(密码){
if($scope.myfrm.textchecker.$dirty&!$scope.myfrm.textchecker.$pristine){
console.log(“!myfrm.textchecker.$dirty+!myfrm.textchecker.$pristine”);
console.log(!myfrm.textchecker.$dirty+!myfrm.textchecker.$pristine);
//var password=$scope.ngdata;
var强度=0;
//console.log(密码);
//警报($scope.ngdata);
//如果(password.length>7)强度+=1
//如果密码同时包含小写和大写字符,请增加强度值
如果(密码匹配(/([a-z].[a-z])|([a-z].[a-z])/)强度+=1
//如果有数字和字符,则增加“强度”值
if(password.match(/([a-zA-Z])/)和&password.match(/([0-9])/)强度+=2
//如果它有一个特殊字符,增加强度值
如果(密码匹配(/([!,%,&,@,#,$,^,*,?,,~])/)强度+=3
//如果有两个特殊字符,则增加“强度”值
如果(password.match(/(.[!,%,&,@,#,$,^,*,?,,~].[!,%,&,@,#,$,^,*,?,,~])/)强度+=5
//现在我们
if(强度2和强度5和强度9){
$scope.final=“Strong”;
}
}
}
});代码>
请输入所需的文本
{{ngdata}}
{{final}}
$dirty:{myfrm.textchecker.$dirty}}$invalid:{{myfrm.textchecker.$invalid}}}$pristine:{{myfrm.textchecker.$pristine}}
只是为了澄清您是否理解正确-即使输入被触摸(键入的任何内容)然后被清除,它仍然被视为$dirty
。因此,换句话说,一旦输入,除非您显式地设置输入(仅清除文本值不会使其变得原始)$dirty
和$pristine
是相反的。你只需要检查其中一个。这样我就可以用$dirty或$dirty了$崭新的如果我使用if($scope.myfrm.textchecker.$dirty&&!$scope.myfrm.textchecker.$invalid)
,它会是一个更好的解决方案吗?很好的捕获,非常感谢。还有没有其他方法可以在不使用正则表达式但使用ng验证的情况下找到密码强度。再次感谢您也许是这样,但您的方法具有灵活性和良好的维护机会,所以我会坚持:)如果它对您有帮助,请接受答案