【发布时间】:2014-08-12 13:36:10
【问题描述】:
试图使 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.router 0.2.10。
【问题讨论】:
-
您测试过我提供的解决方案吗?如果它适合您,请接受“关闭”您的问题的答案。如果您的问题没有解决,请告诉我如何为您提供帮助
标签: javascript angularjs angularjs-directive angular-ui-router