Compare two fields in angularjs directive

≡放荡痞女 提交于 2019-12-05 15:21:58

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();
            });
        }
    };
});

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>

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();
        });
    }

}});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!