角度ng变化延迟

MGo*_*t90 114 angularjs angularjs-directive angularjs-ng-repeat

我有一个输入,可以在更改时过滤ng-repeat列表.重复包含大量数据,需要几秒钟来过滤所有内容.在开始过滤过程之前,我希望它们延迟0.5秒.角度创建此延迟的正确方法是什么?

输入

 <input ng-model="xyz" ng-change="FilterByName()" />
Run Code Online (Sandbox Code Playgroud)

重复

 <div ng-repeat"foo in bar">
      <p>{{foo.bar}}</p>
 </div>
Run Code Online (Sandbox Code Playgroud)

过滤功能

 $scope.FilterByName = function () {
      //Filtering Stuff Here
 });
Run Code Online (Sandbox Code Playgroud)

谢谢

rck*_*ckd 268

AngularJS 1.3+

从AngularJS 1.3开始,您可以利用提供的debounce属性ngModelOptions来实现这一目标,而无需使用$timeout.这是一个例子:

HTML:

<div ng-app='app' ng-controller='Ctrl'>
    <input type='text' placeholder='Type a name..'
        ng-model='vm.name'
        ng-model-options='{ debounce: 1000 }'
        ng-change='vm.greet()'
    />

    <p ng-bind='vm.greeting'></p>
</div>
Run Code Online (Sandbox Code Playgroud)

JS:

angular.module('app', [])
.controller('Ctrl', [
    '$scope',
    '$log',
    function($scope, $log){
        var vm = $scope.vm = {};

        vm.name = '';
        vm.greeting = '';
        vm.greet = function greet(){
            vm.greeting = vm.name ? 'Hey, ' + vm.name + '!' : '';
            $log.info(vm.greeting);
        };
    }
]);
Run Code Online (Sandbox Code Playgroud)

- 要么 -

检查小提琴

在AngularJS 1.3之前

您将不得不使用$ timeout来添加延迟,并且可能使用$ timeout.cancel(previoustimeout)您可以取消任何先前的超时并运行新的超时(有助于防止过滤在多个时间内执行多次时间间隔)

这是一个例子:

app.controller('MainCtrl', function($scope, $timeout) {
    var _timeout;

    //...
    //...

    $scope.FilterByName = function() {
        if(_timeout) { // if there is already a timeout in process cancel it
            $timeout.cancel(_timeout);
        }
        _timeout = $timeout(function() {
            console.log('filtering');
            _timeout = null;
        }, 500);
    }
});
Run Code Online (Sandbox Code Playgroud)

  • 请注意,"ng-model-options"仅在[Angular v1.3]中添加(https://github.com/angular/angular.js/blob/master/CHANGELOG.md#v130-beta6-expedient-caffeination- 2014-04-21)(以及[beta.8]中的去抖属性(https://github.com/angular/angular.js/blob/master/CHANGELOG.md#130-beta8-accidental-haiku-2014- 05-09)).那些仍然需要使用旧版Angular的人将不得不求助于其他解决方案,例如来自PSL的解决方案,或者使用像[ng-debounce]这样的外部模块(https://github.com/shahata/angular-反跳). (2认同)

PSL*_*PSL 19

您可以使用$timeout添加延迟,并且可能使用$timeout.cancel(previoustimeout)您可以取消任何先前的超时并运行新的超时(有助于防止过滤在一个时间间隔内多次执行)

例:-

app.controller('MainCtrl', function($scope, $timeout) {
  var _timeout;

 //...
 //...

  $scope.FilterByName = function () {
    if(_timeout){ //if there is already a timeout in process cancel it
      $timeout.cancel(_timeout);
    }
    _timeout = $timeout(function(){
      console.log('filtering');
      _timeout = null;
    },500);
  }
 });
Run Code Online (Sandbox Code Playgroud)

Plnkr

  • 对于downvoter和未来的访问者:这个答案是为**Angular 1.2.x**添加的,并且可能在1.3.x发布之前添加,它具有ng-model-options的debounce选项,并且从未有机会修改在得到@rckd的更好答案之前回答(在这之后大约3个月). (8认同)
  • 即使我使用角度js 1.4,当我不想去抖动模型时,我仍然发现$ timeout解决方案对于`ng-change`非常有用. (4认同)

Nai*_*Kar 8

我知道这个问题太老了.但仍希望通过使用去抖动来提供一种更快的方法来实现这一点.

所以代码可以写成

<input ng-model="xyz" ng-change="FilterByName()" ng-model-options="{debounce: 500}"/>
Run Code Online (Sandbox Code Playgroud)

去抖将以毫秒为单位.