我有一些基本的问题与形式。我是这么做的。
我从这里截取了这个很酷的指令:https://github.com/TheSharpieOne/angular-input-match
它看起来像这样:
directive('match', function () {
return {
require: 'ngModel',
restrict: 'A',
scope: {
match: '='
},
link: function(scope, elem, attrs, ngModel) {
scope.$watch(function() {
return (ngModel.$pristine && angular.isUndefined(ngModel.$modelValue)) || scope.match === ngModel.$viewValue;
}, function(currentValue, previousValue) {
ngModel.$setValidity('match', currentValue);
});
}
};
});
本质上,该指令监视它所附加的元素的模型值,并将其与match属性中的模型值进行比较。
所以…例如,下面我们将查看两个密码是否匹配:
Password: <input ng-model="password" type="password" />
Confirm: <input ng-model="passwordConfirm" type="password" match="password" />
指令似乎在工作,因为它适当地设置了ng-valid-match和ng-invalid-match。
然而,一旦它被设置为无效,passwordConfirm模型就不会再次更新。我做过很多主机游戏。登录,查看指令中的ngModel,下面是两个密码匹配时的样子:
Constructor {$viewValue: "asdf", $modelValue: undefined, $validators: Object, $parsers: Array[0], $formatters: Array[0]…}
$$debounceViewValueCommit: function (trigger, revalidate) {
$$invalidModelValue: "asdf"
$$lastCommittedViewValue: "asdf"
$$runValidators: function (modelValue, viewValue) {
$$validityState: ValidityState
$$writeModelToScope: function () {
$commitViewValue: function (revalidate) {
$dirty: true
$error: Object
$formatters: Array[0]
$invalid: false
$isEmpty: function (value) {
$modelValue: undefined
$name: "passwordConfirmation"
$parsers: Array[0]
$pristine: false
$render: function () {
$rollbackViewValue: function () {
$setPristine: function () {
$setTouched: function () {
$setUntouched: function () {
$setValidity: function (validationErrorKey, isValid) {
$setViewValue: function (value, trigger, revalidate) {
$touched: true
$untouched: false
$valid: true
$validate: function () {
$validators: Object
$viewChangeListeners: Array[0]
$viewValue: "asdf"
__proto__: Object
注意,$viewValue是正确的,但$modelValue被列为未定义和$invalidModelValue仍然有一个值。
当两个密码匹配时,html是这样的:
<input type="password" class="form-control ng-isolate-scope ng-dirty ng-valid-required ng-valid ng-valid-match ng-touched" id="passwordConfirmation" name="passwordConfirmation" placeholder="Confirm your password" ng-model="passwordConfirmation" required="" match="password" style="">
我错过了什么地方吗?我已经绕了好几个小时了
在最近的更新中,根据字段的有效性对$modelValue
的填充方式进行了更改。如果该字段无效,则将$modelValue
设置为undefined
,并使用该值填充新属性$$invalidModelValue
。
作为与1.2一起工作的解决方案。*和1.3。*我想到了这个:
.directive('match', function () {
return {
require: 'ngModel',
restrict: 'A',
scope: {
match: '='
},
link: function(scope, elem, attrs, ctrl) {
scope.$watch(function() {
modelValue = ctrl.$modelValue || ctrl.$$invalidModelValue;
return (ctrl.$pristine && angular.isUndefined(modelValue)) || scope.match === modelValue;
}, function(currentValue) {
ctrl.$setValidity('match', currentValue);
});
}
};
});
Plunkr
虽然此解决方案适用于两个版本,但1.3。*有新的$validators
管道,这是推荐的新版本。
看起来可能使用$setValidity不是这里的方式。我发现这个问题提出了一个不同的解决方案,使用$validators和$validate(),这对我来说很有效。新代码看起来像这样:
directive('match', function () {
return {
require: 'ngModel',
restrict: 'A',
scope: {
match: '='
},
link: function(scope, elem, attrs, ngModel) {
scope.$watch('match', function(pass){
ngModel.$validate();
});
ngModel.$validators.match = function(modelValue, viewValue){
var value = modelValue || viewValue;
var match = scope.match;
return value === match;
};
}
};
});
可能与您正在使用
有关scope: {
match : "="
}
为你的指令创建一个独立的作用域,并且不会从你的ngModel所在的父作用域继承。
我建议尝试删除指令的作用域部分,并从属性中访问它。
它将变成类似于:
directive('match', function () {
return {
require: 'ngModel',
restrict: 'A',
link: function(scope, elem, attrs, ngModel) {
scope.match = attrs.match;
scope.$watch(function() {
return (ngModel.$pristine && angular.isUndefined(ngModel.$modelValue)) || scope.match === ngModel.$viewValue;
}, function(currentValue, previousValue) {
ngModel.$setValidity('match', currentValue);
});
}
};
});