Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了angularjs – 角度ng变化延迟大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个输入,过滤ng重复列表的变化。重复包含大量数据,并需要几秒钟来过滤一切。我希望他们的0.5秒延迟,我开始过滤过程。在创建这个延迟的角度的正确方法是什么?

输入

<input ng-model="xyz" ng-change="FilterByName()" />

重复

<div ng-repeat"foo in bar">
      <p>{{foo.bar}}</p>
 </div>

过滤功能

$scope.FilterByName = function () {
      //Filtering stuff Here
 });

谢谢

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>

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

– 要么 –

Check the Fiddle

之前的AngularJS 1.3

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

这里是一个例子:

app.controller('MainCtrl',$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);
    }
});

大佬总结

以上是大佬教程为你收集整理的angularjs – 角度ng变化延迟全部内容,希望文章能够帮你解决angularjs – 角度ng变化延迟所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。