简体   繁体   English

filter:notarray预期数组但收到:0

[英]filter:notarray Expected array but received: 0

controller 调节器

    @RequestMapping(value = "/graphs", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public Collection<Graph> getSkeletonGraph()
    {
        log.debug("REST request to get current graphs");
        return graphService.getSkeletonGraphs();
    }

Angular call 角度呼叫

    $scope.graphs = [];
    Graph.getGraphs().$promise.then(function(result)
    {
        $scope.graphs = result;
    });



    angular.module('sampleApplicationApp').factory('Graph', function($resource)
     {
      return {
        getGraphs: function() {
           return    $resource('api/graphs/:id').query();
      }
     };
    })

I am not sure why using the filter i get the exception. 我不知道为什么使用过滤器我得到了例外。

looked also in angular doc https://docs.angularjs.org/error/filter/notarray My result is array but not sure why I am getting such exception. 也看了角度文档https://docs.angularjs.org/error/filter/notarray我的结果是数组,但不知道为什么我得到这样的异常。

Sample result from backend i am getting. 来自后端的示例结果我得到了。

[{"id":"135520b0-9e4b-11e5-a67e-5668957d0149","area":"Bingo","models":[],"enumerateds":[]},{"id":"0db925e0-9e53-11e5-a67e-5668957d0149","area":"jin","models":[],"enumerateds":[]},{"id":"7a717330-9788-11e5-b259-5668957d0149","area":"Product","models":[],"enumerateds":[]},{"id":"402d4c30-980f-11e5-a2a3-5668957d0149","area":"fgfgfg","models":[],"enumerateds":[]},{"id":"404b77b0-9e53-11e5-a67e-5668957d0149","area":"olah","models":[],"enumerateds":[]},{"id":"cd071b10-9e52-11e5-a67e-5668957d0149","area":"lolo","models":[],"enumerateds":[]},{"id":"d9808e60-9710-11e5-b112-5668957d0149","area":"catalog","models":[],"enumerateds":[]},{"id":"2aaca9f0-97e2-11e5-91cd-5668957d0149","area":"btg","models":[],"enumerateds":[]},{"id":"955e9ed0-978c-11e5-93fd-5668957d0149","area":"promotions","models":[],"enumerateds":[]},{"id":"1e441d60-980f-11e5-a2a3-5668957d0149","area":"hjuhh","models":[],"enumerateds":[]},{"id":"fb96dfe0-978d-11e5-93fd-5668957d0149","area":"voucher","models":[],"enumerateds":[]}]

html HTML

<li ng-repeat="g in graphs track by $index | filter:searchText"></li>

The problem is occurring because you are using track by $index before you are applying your filter. 出现此问题的原因是您在应用过滤器之前使用track by $index To resolve this, change your expression to: 要解决此问题,请将表达式更改为:

<li ng-repeat="g in graphs | filter:searchText track by $index"></li>

The track by expression should always be at the last, after all your filters. 在所有过滤器之后, track by expression应始终位于最后。 Its a rule mentioned in the docs: ngRepeat 它是文档中提到的规则: ngRepeat

Explanation: 说明:

When you don't use track by $index in ngRepeat , the input for all the filters used is the array, that is, if its 如果你没有在ngRepeat使用track by $indexngRepeat所使用的所有过滤器的输入都是数组,也就是说,如果是

ng-repeat="item in items | filter1 | filter2", 

then items is the input passed to the filters by default and the filtering is done on this input. 那么items是默认传递给过滤器的输入,并且对此输入进行过滤。

However, when you use track by $index , the input to the filters becomes $index instead of items and therefore the error: 但是,当您使用track by $index时,过滤器的输入变为$index而不是items ,因此错误:

Expected array(read: items) but received 0(read: $index). 预期数组(读取:项目)但收到0(读取:$ index)。

Therefore, to counter this, the array is first passed through all the filters and the filtered result is used with track by $index . 因此,为了解决这个问题,首先将数组传递给所有过滤器,并将过滤后的结果与track by $index

Hope this clears it up. 希望这清除它。

You should always use track by at the end of expression 您应该始终在表达式结尾处使用track by

<li ng-repeat="g in graphs | filter:searchText track by $index"></li>

Since while evaluating an expression for ng-repeat angular needs the final result for track by to work. 因为在评估ng-repeat angular的表达式时需要跟踪的最终结果才能工作。 If you provide it at the end, your filter will be applied and track by is computed on final output. 如果您在最后提供它,则将应用您的过滤器,并在最终输出上计算track by You can see the source code at angular docs. 您可以在angular docs上看到源代码。

According to the documentation of ng-repeat 根据ng-repeat的文档

If you are working with objects that have an identifier property, you should track by the identifier instead of the whole object. 如果使用具有标识符属性的对象,则应按标识符而不是整个对象进行跟踪。 Should you reload your data later, ngRepeat will not have to rebuild the DOM elements for items it has already rendered, even if the JavaScript objects in the collection have been substituted for new ones. 如果您以后重新加载数据,ngRepeat将不必为已经呈现的项重建DOM元素,即使集合中的JavaScript对象已替换为新对象。 For large collections, this signifincantly improves rendering performance. 对于大型集合,这显着提高了渲染性能。 If you don't have a unique identifier, track by $index can also provide a performance boost. 如果您没有唯一标识符,则通过$ index跟踪也可以提高性能。

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

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