简体   繁体   English

使用过滤器延迟ng-repeat结果

[英]Delaying ng-repeat result using filter

My problem is I want to delay the ng-repeat result in angularjs. 我的问题是我想延迟angularjs中的ng-repeat结果。 My code for the delaying filter is below. 我的延迟过滤器代码如下。

    <ul>
     <li ng-repeat="phone in phones | filter:delay_filter">
     <span>{{phone.name}}</span>
     </li>
     </ul>
     <script>
 var phonecatApp = angular.module('phonecatApp', ['ngMessages']);
phonecatApp.controller('PhoneListCtrl', function ($scope, $http, $scope) {
     $scope.delay_filter=function(item){
        alert(++$c);
        x=setTimeout($scope.trf(),2200);
        return item;
    };};)
     </script>

But this doesn't work. 但这是行不通的。 The search gives instantaneous results. 搜索给出瞬时结果。 I didn't write controller in the html and I know that. 我没有在html中编写控制器,我知道这一点。 In my code its there. 在我的代码中。 Thats not the problem. 那不是问题。 Please help me with this problem 请帮我解决这个问题

I think you should make a directive or a separate filter using angular.filter. 我认为您应该使用angular.filter来创建指令或单独的过滤器。 Otherwise if you want this to work you can use $timeout 否则,如果您希望此方法有效,则可以使用$ timeout

You can extract to a directive like this ( http://jsfiddle.net/xL02yhzz/3/ ): 您可以提取到这样的指令( http://jsfiddle.net/xL02yhzz/3/ ):

angular.module('Joy', [])
.controller('RepeatCtrl', ['$scope', '$timeout', function ($scope, $timeout) {
    $scope.data = [1, 2, 3, 5, 8, 13, 21, 34];
}])
.directive('delayRepeat', [function () {
    return {
        restrict: 'A',
        scope: {
            myListData: '='
        },
        template:
            '<div>' +
            '<ul>' +
            '<li ng-repeat="d in myListData" ng-bind="d"></li>' +
            '</ul>' +
            '</div>',
        controller: ['$scope', '$timeout', function ($scope, $timeout) {
            $timeout(function () {
                $scope.myListData = [55, 89, 144, 233, 377];
            }, 5000);
        }]
    };
}]);

The HTML is: HTML是:

<div ng-app="Joy">5 seconds will change:
    <div ng-controller="RepeatCtrl">
        <div delay-repeat my-list-data="data"></div>
    </div>
</div>

Don't do that in the filter. 不要在过滤器中这样做。 I's absolutely not its responsibility to delay rendering. 延迟渲染绝对不是我的责任。 You should write a directive instead. 您应该改为编写指令。

If you really insist on doing it this way, use a flag that you update after a delay using $timeout , and make the filter return results only if the flag is set to true : 如果您确实坚持这样做,请使用在$timeout延迟后更新的标志,并且仅当该标志设置为true才使过滤器返回结果:

var delayInMilliseconds = 3000;
var doneWaiting = false;
$scope.delay_filter= function(item){
    return doneWaiting;
};

$timeout(function() {
    doneWaiting = true;
}, delayInMilliseconds);

Working example here . 这里的工作示例。

Additional advice : you should prefer $timeout over setTimeout when coding in Angular, as it handles the digesting of changes once the parameter function is executed. 附加建议:在Angular中进行编码时,您应该更喜欢$timeout不是setTimeout ,因为一旦执行参数函数,它就可以处理更改的摘要。 The vanilla version forces you to call $scope.$apply() to update your model. 原始版本会强制您调用$scope.$apply()来更新模型。

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

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