简体   繁体   English

AngularJS自定义过滤器被调用两次,并在第二次调用时删除输入数据

[英]AngularJS custom filter called twice and delete input data on second call

Here are the codes. 这是代码。

var app = angular.module("nameApp", ["ngRoute"]);

app.controller("ctrlname", function ($scope, $http, $filter, apiKey, apiUrl) {
    $scope.data = {};
    $scope.currentPage = 1;
    $scope.pageSize = 5;
});

The $scope.data will contain an array of data from an HTTP GET request. $ scope.data将包含来自HTTP GET请求的数据数组。 The following is a code for a custom filter for the purpose of pagination of results. 以下是用于分页结果的自定义过滤器的代码。 Basically, this will limit the results to only 5. Buttons for pagination will update $scope.currentPage's value. 基本上,这会将结果限制为5。分页按钮将更新$ scope.currentPage的值。

app.filter("limitResults", function ($filter, $log) {
    return function (data, page, size) {
        if (angular.isArray(data) & angular.isNumber(page) && angular.isNumber(size)) {
            var startPage = (page - 1) * size;
            if (data.length < startPage) {
                return [];
            } else {
                $log.info(data);
                $log.info(page);
                $log.info(size);
                $log.info(startPage);
                return $filter("limitTo")(data.splice(startPage), size);
            }
        } else {
            return data;
        }
    }
});

This is the HTML page that will render the data. 这是将呈现数据的HTML页面。

<div class="row resultItems" ng-repeat="video in data.videos | limitResults:currentPage:pageSize">
    <div class="col-sm-3 testing">
        <img ng-src="{{video.snippet.thumbnails.default.url}}">
    </div>
    <div class="col-sm-9 testing">
        <h5>
            {{video.snippet.title}}
        </h5>
        <p>
            {{video.snippet.channelTitle}}
        </p>
        <p>
            {{video.snippet.description}}
        </p>
    </div>
</div>

I put a few lines of $log.info code in the custom filter in order to see what really happens when the filter is applied. 我在自定义过滤器中放置了几行$ log.info代码,以了解应用过滤器时实际发生的情况。 The filter runs twice, which is a normal behaviour. 过滤器运行两次,这是正常现象。

What I find confusing is that when the custom filter runs for the first time, $log.info(data) logs the original data received from a HTTP GET call to the console. 我感到困惑的是,自定义过滤器首次运行时,$ log.info(data)将从HTTP GET调用接收到的原始数据记录到控制台。 However, when the custom filter runs for the second time, $log.info(data) logs an empty array to the console. 但是,当自定义过滤器第二次运行时,$ log.info(data)将一个空数组记录到控制台。

Given the fact that "$log.info(data); $log.info(page); $log.info(size);" 鉴于以下事实:“ $ log.info(data); $ log.info(page); $ log.info(size);” get logged to the console, it is obvious that the second IF statement (if (data.length < startPage)) is evaluated to TRUE and the filter (return $filter("limitTo")(data.splice(startPage), size);) is applied. 记录到控制台后,很明显,第二条IF语句(如果(data.length <startPage))被评估为TRUE,并且过滤器(返回$ filter(“ limitTo”)(data.splice(startPage),size) ;) 被申请;被应用。

I just don't understand why the array, which is the data passed to the custom filter, gets emptied when the filter runs the second time. 我只是不明白为什么数组(传递给自定义过滤器的数据)在第二次运行时会被清空。

The reason you are seeing empty array is because of the splice method. 您看到空数组的原因是由于splice方法。

$filter("limitTo")(data.splice(startPage), size);

Splice method syntax 拼接方法语法

array.splice(start, deleteCount[, item1[, item2[, ...]]])

If splice method is called without second parameter, that means if deleteCount is not passed, deleteCount will be treated as [arr.length - start]. 如果在没有第二个参数的情况下调用splice方法,则意味着如果未传递deleteCount,则deleteCount将被视为[arr.length-start]。 In your case, when the first time filter executes, the entire array becomes empty. 在您的情况下,当第一次执行过滤器时,整个数组将为空。

See this doc for splice method 有关接头方法, 请参阅此文档

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

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