Angular - ng-change not firing when ng-model is changed

后端 未结 4 1663
伪装坚强ぢ
伪装坚强ぢ 2020-12-15 19:17

The input is the following:


The action() is exe

4条回答
  •  Happy的楠姐
    2020-12-15 19:52

    Another solution would be to use a directive that watched the model for any changes instead of using ng-change.

    app.directive('onModelChange', function($parse){
        return {
            restrict: "A",
            require: "?ngModel",
            link: function(scope, elem, attrs, ctrl) {
                scope.$watch(attrs['ngModel'], function (newValue, oldValue) {
                    if (typeof(newValue) === "undefined" || newValue == oldValue) {
                        return;
                    }
                    var changeExpr = $parse(attrs['onModelChange']);
                    if (changeExpr) {
                        changeExpr(scope);
                    }
                });
            } 
        };
    });
    

    Then you would use it like so:

    
    

提交回复
热议问题