Angularjs setValidity causing modelValue to not update

前端 未结 3 1648
既然无缘
既然无缘 2021-01-03 01:00

I\'m having some basic trouble with a form. Here\'s what I did.

I snagged this cool looking directive from here: https://github.com/TheSharpieOne/angular-input-matc

3条回答
  •  礼貌的吻别
    2021-01-03 01:15

    It looks like maybe using $setValidity is not the way to go here. I found this question that proposes a different solution, using $validators and $validate(), and this is working for me great. The new code looks like this:

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

提交回复
热议问题