简体   繁体   中英

How can I use onSelect instead of ng-change in my select tag using AngularJS

I want to use the onSelect event for my select tag instead of ng-change event.

I face some issue by using ng-change event on my code while I using update the onChange Function call automatically on set value in my update page and I have some calculation in that function so the calculation gets call automatically and many things connected with one function.

so I want to call function only when I select any value from select box please response if you have a proper good solution.

You need to add Directive in your Input Tag.

If you add the directive to your code you would change your binding to this:

<input type="text" ng-model="name" ng-model-onblur ng-change="update()" />

Here is the directive:

// override the default input to update on blur

angular.module('app', []).directive('ngModelOnblur', function() {
    return {
        restrict: 'A',
        require: 'ngModel',
        priority: 1, // needed for angular 1.2.x
        link: function(scope, elm, attr, ngModelCtrl) {
            if (attr.type === 'radio' || attr.type === 'checkbox') return;

            elm.unbind('input').unbind('keydown').unbind('change');
            elm.bind('blur', function() {
                scope.$apply(function() {
                    ngModelCtrl.$setViewValue(elm.val());
                });         
            });
        }
    };
});

Reff: Angularjs: input[text] ngChange fires while the value is changing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM