无法将滚动函数绑定到 AngularJS 中的指令



我在我的 MEAN 堆栈应用程序中为下拉列表编写了一个自定义指令,但无法将滚动函数绑定到它。这是指令代码:

.directive('inputDropdown', function($parse) {
    var template = 
        '<input class="form-control" ng-model="ngModel" ng-disabled="disabled"  placeholder="Type Name">' +
        '<div class="dropdown dropdown1">' + 
            '<div class="form-control" ng-repeat="value in selectedList | filter:ngModel | limitTo:limit">' +
                '<div ng-mousedown="select($event, value)">{{value}}</div>' + 
            '</div>' +
        '</div>';
    return {
        restrict: 'EA',
        require: '^form',
        scope: {
            ngModel: '=',
            list: '=',
            onSelect: '&',
            disabled:'=ngDisabled'
        },
        template: template,
        link: function(scope, element, attrs,mapController) {
            element.addClass('input-dropdown');
            angular.element(document.querySelector('input-dropdown')).bind('scroll', function(){ //1st way
                alert('scrolling is cool!');
              });
         //   element.scroll(function (evt) { //2nd way
          //      alert('scrolling is nama-cool!');
         //   });
            if(scope.$parent.setDirty)
            {
                scope.makeFormDirty = mapController.$setDirty();
            }
            scope.select = function(e, value) {
                scope.ngModel = value;
                // scope.onSelect({$event: e, value: value});
                scope.makeFormDirty = mapController.$setDirty();
            };
        }
    };
})

我尝试通过两种方式绑定滚动函数,但没有一种工作。

第一种方式:

angular.element(document.querySelector('input-dropdown')).bind('scroll', function(){ //1st way
                alert('scrolling is cool!');
              });

第二种方式:

        element.scroll(function (evt) { //2nd way
            alert('scrolling is nama-cool!');
        });

您似乎缺少'.input-dropdown'并使用.on('scroll')

.bind已被弃用,则应使用 .on 。查看文档

它应该是

angular.element(document.querySelector('.input-dropdown')).on('scroll', function(){ //1st way
                alert('scrolling is cool!');
              });

最新更新