Angular.js - ng-change not firing when ng-pattern is $invalid

后端 未结 4 2109
别那么骄傲
别那么骄傲 2020-12-13 06:43

I am using ng-pattern to validate some form fields, and I am using ng-change with it to watch and process any changes, however ng-change (or $scope.$watch) will only fire wh

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-13 06:53

    Edit This was answered when ng-model-options was not available. Please see the top-voted answer.

    you can write a simple directive to listen input event.

    HTML:

     Changes: {{ changes }}
    

    JS:

    app.directive('watchChange', function() {
        return {
            scope: {
                onchange: '&watchChange'
            },
            link: function(scope, element, attrs) {
                element.on('input', function() {
                    scope.$apply(function () {
                        scope.onchange();
                    });
                });
            }
        };
    });
    

    http://jsfiddle.net/H2EAB/

提交回复
热议问题