问题
I am trying to create directive which can be used to compare two fields in multiple projects.
MarkUp :
<div class="form-group">
<input ng-model="user.password" type="password" name="password" />
</div>
<div class="form-group">
<input ng-model="user.confpassword" ng-compare="password" name="confpassword" type="password" />
<p ng-show="registrationform.password.$error.ngcompare" class="help-block">Password's don't match</p>
Directive :
"use strict";
angular.module('app.directive.ngCompare', []).directive('ngCompare', function () {
return {
require: 'ngModel',
link: function (scope, element, attrs, ngModelController)
{
ngModelController.$parsers.unshift(function (viewvalue) {
console.log(scope); // doesnot contain password field object
console.log(viewvalue); // gives me value of confpassword field
console.log(scope[attrs.ngCompare]); // undefined
});
}
}});
I have not completed writing my directive but , during development when i console scope i dont get value of first password but i get value of confpassword.i am including this direcitive in my main app as 'app.directive.ngCompare'.Is it because of that i dont get value of password.
I am using angular version 1.3.9. I know there are many similar directives out there but i need to figure out step by step how they run so started creating from scratch.Is there any other way to get value of password using angularjs techiques rather than jquery methods.
回答1:
The problem with the answers given so far is that they all create an isolate scope. This means you couldn't use additional directives on the same input or on another directive.
That can be fixed by modifying the above as follows:
.directive("compareTo", function() {
return {
require: "ngModel",
link: function(scope, element, attrs, ctrl) {
ctrl.$validators.compareTo = function(val) {
return val == scope.$eval(attrs.compareTo);
};
scope.$watch(attrs.compareTo, function() {
ctrl.$validate();
});
}
};
});
回答2:
It might help you!!
<!DOCTYPE html>
<html ng-app="app">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.css" />
<script src="https://code.angularjs.org/1.3.0-rc.2/angular.js"></script>
<style>
.ng-invalid {
border-color: red;
outline-color: red;
}
.ng-valid {
border-color: green;
outline-color: green;
}
</style>
<script>
var app = angular.module("app", []);
app.controller("confirmCtrl", function($scope) {
$scope.user = {
password: "",
confirmPassword: ""
};
});
app.directive("compareTo", function() {
return {
require: "ngModel",
scope: {
confirmPassword: "=compareTo"
},
link: function(scope, element, attributes, modelVal) {
modelVal.$validators.compareTo = function(val) {
return val == scope.confirmPassword;
};
scope.$watch("confirmPassword", function() {
modelVal.$validate();
});
}
};
});
</script>
</head>
<body ng-controller="confirmCtrl">
<form>
<lable></lable>
<div>
<label>Password</label>
<input type="password" name="pwd" ng-model="user.password" required class="form-control"/>
</div>
<div>
<label>Confirm Password</label>
<input type="password" name="confirmPassword" ng-model="user.confirmPassword" required compare-to="user.password" class="form-control"/>
</div>
</form>
</body>
</html>
回答3:
Firstly, avoid using 'ng' as a prefix for a custom directive, as this could clash with any future directives angular decide to add in the future.
Compare to the ng-model instead of the field name in the HTML:
<input ng-model="user.confpassword" ss-compare="user.password" name="confpassword" type="password" />
Then add a new scope for the directive, in which you pass in the password:
return {
require: 'ngModel',
scope: {
ssCompare: '='
},
link: function (scope, element, attrs, ngModelController)
{
ngModelController.$validators.compareTo = function(modelValue) {
return modelValue == scope.ssCompare;
};
scope.$watch("ssCompare", function() {
ngModelController.$validate();
});
}
}});
来源:https://stackoverflow.com/questions/28063107/compare-two-fields-in-angularjs-directive