Angularjs检查密码的指令已经重复,不能工作
angularjs directive to check password has been repeated not working
尝试使AngularJS指令工作,我遵循这个教程:http://sahatyalkabov.com/create-a-tv-show-tracker-using-angularjs-nodejs-and-mongodb/
在注册表单中,名为repeat-password的指令检查表单中输入的两个电子邮件是否对应:
<div class="form-group" ng-class="{ 'has-success' : signupForm.confirmPassword.$valid && signupForm.confirmPassword.$dirty, 'has-error' : signupForm.confirmPassword.$invalid && signupForm.confirmPassword.$dirty }">
<input class="form-control input-lg" type="password" name="confirmPassword" ng-model="confirmPassword" repeat-password="password" placeholder="Confirm Password" required>
<div class="help-block text-danger my-special-animation" ng-if="signupForm.confirmPassword.$dirty"ng-messages="signupForm.confirmPassword.$error">
<div ng-message="required">You must confirm password.</div>
<div ng-message="repeat">Passwords do not match.</div>
</div>
</div>
<button type="submit" ng-disabled="signupForm.$invalid" class="btn btn-lg btn-block btn-primary">Create Account</button>
所需的html的部分工作,但不是重复密码部分,没有错误信息显示,它不阻止我提交的形式,我直接路由到提交按钮导致太。我使用的指令本身的代码如下:
angular.module("MyApp")
.directive("repeatPassword", function () {
return {
require: "ngModel",
link: function (scope, elem, attrs, ctrl) {
var otherInput = elem.inheritedData("$formController")[attrs.repeatPassword];
ctrl.$parsers.push(function (value) {
if (value === otherInput.$viewValue) {
ctrl.$setValidity("repeat", true);
return value;
}
ctrl.$setValidity("repeat", false);
});
otherInput.$parsers.push(function (value) {
ctrl.$setValidity("repeat", value === ctrl.$viewValue);
return value;
});
}
};
});
我不知道它是否有用,但我使用AngularJS v1.3.0-beta。15和Angular-ui。路由器0.2.10。
这是一个通用版本的fieldMatch指令,带有注释和对其他SO问题的引用。它已经在1.2和1.3测试版中进行了测试。
.directive('fieldMatch', function () {
return {
restrict: 'A',
scope: true,
require: 'ngModel',
link: function (scope, elem, attrs, ngModel) {
var checkFieldMatch = function () {
// WARNING - FIXME:
// when a validator fails (returns false), then the underlying model value is set to undefined
//
// https://stackoverflow.com/questions/24692775
// https://stackoverflow.com/questions/24385104
//
// => solution: use ngModel.$viewValue instead of ngModel.$modelValue
var fieldMatch = scope.$eval(attrs.dsFieldMatch),
value = ngModel.$modelValue || ngModel.$viewValue;
// If fieldMatch or ngModel.$viewValue is defined,
// they should match
if (fieldMatch || value) {
return fieldMatch === value;
}
return true;
};
scope.$watch(checkFieldMatch, function (n) {
//set the form control to valid if both
//passwords are the same, else invalid
ngModel.$setValidity('fieldMatch', n);
});
}
};
});
参见以下两个问题:
Angularjs setValidity导致modelValue不更新
在AngularJS中使用$validators管道进行密码匹配会产生意想不到的结果
相关文章:
- 用javascript将script元素附加到头部;铬不能工作
- 为什么jQuery代码段在没有IFrame的情况下可以工作,而在有IFrame时却不能工作
- 为什么当async标志设置为false时,xmlhttprequest中的代码可以工作,而当它设置为true时却不能工作
- 引导程序's的javascript在本地工作,但在部署到服务器时不能工作
- JQuery脚本没有'Don’我不能工作两次
- "npm运行构建:css"不能工作,而当我自己运行脚本时是可以的
- ng样式在与的预标记中!重要的是不能工作
- angularjs 控制器在指令中使用控制器作为不能工作
- jQuery LightSlider不能工作,但在JSFiddle中工作得很好
- Javascript - 我如何让一个语句工作一次,如果它已经工作了 1 次,则不能工作,然后再次工作
- 我的查询获胜'因为一个剧本,它不能工作(它以错误的方式工作)!?但为什么呢?SESSIONS和PHP
- 我的Javascript代码没有'铬不能工作
- 使用JQuery调整图像大小和重新排列图像;第一次装载时不能工作
- 为什么不是'这个Javascript计算器不能工作
- Javascript在窗口中返回.onbeforeunload不会'铬不能工作
- Javascript代码包含在html中时可以工作,但单独加载/注入时则不能工作
- 简单的jquery选择器不会;铬不能工作
- 为什么不'这个Javascript函数不能工作
- 为什么不'这个js数组函数不能工作
- 视口比例;方向改变后不能工作