简体   繁体   English

我如何使用AngularJS在select标签中使用onSelect而不是ng-change

[英]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. 我想将onSelect事件用于我的选择标签,而不是ng-change事件。

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. 我在代码中使用ng-change事件时遇到了一些问题,同时我在更新页面上对设置值进行自动更新时对onChange函数进行了调用,并且该函数中进行了一些计算,因此该计算会自动获得调用,并且许多事情都与一个函数有关。

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 Reff: Angularjs:输入[text] ngChange在值更改时触发

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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