Can anyone explain me on how this piece of code works.
HTML Markup
<input type="password" ng-model="password" class="form-control" placeholder="Password" required>
<input type="password" ng-model="confirm_password" class="form-control" placeholder="Password" required validate-equals="password">
Directive Code
angular.module('app')
.directive('validate', function () {
return {
require: "ngModel",
link: function postLink(scope, element, attrs, ngModelCtrl) {
function validate(value){
var valid = (value === scope.$eval(attrs.validate));
ngModelCtrl.$setValidtity('equal', valid);
return valid ? value : undefined;
}
ngModelCtrl.$parsers.push(validate);
ngModelCtrl.$formatters.push(validate);
$scope.$watch(attrs.validate, function(){
ngModelCtrl.$setViewValue(ngModelCtrl.$viewvalue);
})
}
};
});
Can anyone explain me the below questions.
What does the below code do in the directive?.
$scope.watch(attrs.validate, function(){
ngModelCtrl.$setViewValue(ngModelCtrl.$viewvalue);
});
How is the value passed to validate() function?.