Angularjs Angular ngModel.$validate()在指令中未定义
我正在尝试使用angular集成一个简单的密码验证检查。 出于某种原因,无论我做什么,都没有定义ngModel.$validate(),因此实际上没有进行验证。任何帮助都是徒劳的。 我有一个自定义控制器:Angularjs Angular ngModel.$validate()在指令中未定义,angularjs,angularjs-directive,Angularjs,Angularjs Directive,我正在尝试使用angular集成一个简单的密码验证检查。 出于某种原因,无论我做什么,都没有定义ngModel.$validate(),因此实际上没有进行验证。任何帮助都是徒劳的。 我有一个自定义控制器: angular.module('todo') .controller('RegisterController', ['$scope', '$http', '$location', function($scope, $http, $location) { $scope.r
angular.module('todo')
.controller('RegisterController', ['$scope', '$http', '$location', function($scope, $http, $location) {
$scope.register = function () {
});
我有一个路由器:
.config(function($routeProvider) {
$routeProvider.
when('/todo', {
templateUrl: './partials/todo.html',
controller: 'TodoController'
}).
when('/register', {
templateUrl: './partials/register.html',
controller: 'RegisterController'
}).
otherwise({
redirectTo: '/todo'
});
});
我有一个指示:
var compareTo = function() {
return {
require: "ngModel",
scope: {
otherModelValue: "=compareTo"
},
link: function(scope, element, attributes, ngModel) {
ngModel.$validators.compareTo = function(modelValue) {
return modelValue == scope.otherModelValue;
};
scope.$watch("otherModelValue", function() {
console.log('in otherModelValue ');
console.log('ngModel.$validate() : ' + ngModel.$validate());
ngModel.$validate();
});
}
};
};
angular.module('todo').directive("compareTo", compareTo);
我以以下方式将其全部绑定在一起:
<input id="password" name="password" type="password" placeholder="Password" ng-model="password" required/> <br/>
<input id="passwordConfirm" type="password" placeholder="Repeat Password" ng-model="confirmPassword" required compare-to="password" /> <br/>
ngModel.$validate()
应该由设计来定义,因为它不返回任何特定值。您确定您的验证无效,但无法显示该验证吗?尝试添加到模板中(假设您的表单具有name=“form”
):
密码必须匹配
另外,在confirmPassword字段中添加
confirmPassword
的name
属性我相信/猜测您实际报告的是ngModel。$validate-函数,而不是返回值-未定义。你找到解决办法了吗?你是如何解决这个问题的?