gpt4 book ai didi

javascript - 过滤器 :notarray Expected array but received: 0

转载 作者:可可西里 更新时间:2023-11-01 01:19:56 30 4
gpt4 key购买 nike

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 调用

    $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();
}
};
})

我不确定为什么使用过滤器会出现异常。

也在 Angular 文档中查看 https://docs.angularjs.org/error/filter/notarray我的结果是数组,但不确定为什么会出现这样的异常。

我从后端得到的示例结果。

[{"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

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

最佳答案

出现此问题是因为您在应用过滤器之前使用了 track by $index。要解决此问题,请将您的表达式更改为:

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

track by 表达式应该始终放在最后,在所有过滤器之后。这是文档中提到的规则:ngRepeat

解释:

当你在ngRepeat中不使用track by $index时,所有使用的过滤器的输入都是数组,也就是说,如果它的

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

then items 是默认传递给过滤器的输入,过滤是在这个输入上完成的。

但是,当您使用 track by $index 时,过滤器的输入变为 $index 而不是 items,因此错误:

Expected array(read: items) but received 0(read: $index).

因此,为了解决这个问题,数组首先通过所有过滤器,过滤后的结果用于track by $index

希望这能解决问题。

关于javascript - 过滤器 :notarray Expected array but received: 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34175783/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com