Angularjs setValidity causing modelValue to not update

前端 未结 3 1676
既然无缘
既然无缘 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:17

    In a recent update, a change was made to the way the $modelValue is populated based on the validity of the field. If the field is invalid, the $modelValue will be set to undefined and a new attribute, $$invalidModelValue will be populated with the value.

    As a solution to work with 1.2.* and 1.3.* I have come up with this:

     .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

    While this solution works with both version, 1.3.* has the new $validators pipeline which is recommended for the new version.

提交回复
热议问题