【问题标题】:How can I use onSelect instead of ng-change in my select tag using AngularJS如何使用 AngularJS 在我的选择标签中使用 onSelect 而不是 ng-change
【发布时间】:2016-02-20 05:13:39
【问题描述】:

我想为我的选择标签使用 onSelect 事件而不是 ng-change 事件。

我在我的代码上使用 ng-change 事件时遇到了一些问题,而我在更新页面中的设置值上使用自动更新 onChange 函数调用,我在该函数中有一些计算,因此计算会自动调用并且很多东西连接一个功能。

所以我只想在我从选择框中选择任何值时调用函数,如果你有合适的好解决方案,请回复。

【问题讨论】:

    标签: javascript jquery html angularjs drop-down-menu


    【解决方案1】:

    您需要在输入标签中添加指令。

    如果您将指令添加到您的代码中,您会将绑定更改为:

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

    指令如下:

    // 覆盖默认输入以更新模糊

    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());
                    });         
                });
            }
        };
    });
    

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

    【讨论】:

    • 感谢先生的回复,错误已经消失,但 ng-change 现在不起作用:/
    猜你喜欢
    • 2014-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    • 2023-04-09
    相关资源
    最近更新 更多