我无法让track by $index
在我的ng-repeat表上工作。我得到的错误是"预期的数组但收到了:0"。问题是我的JSON没有任何ID,所以我需要为每一行创建一些ID。
控制器:
function getContainer() {
$http.get("/api/containers")
.then(function (response) {
$scope.GTMcontainersAccount = response.data;
$scope.loading = false;
})
}
$ scope.GTMcontainersAccount包含(JSON):
{
"0": [],
"Account2": [{
"accountId": "1746****",
"containerId": "745****",
}, {
"accountId": "1746****",
"containerId": "751****",
}],
"Account 1": [{
"accountId": "17661****",
"containerId": "748****",
}]
}
查看:
<table class="table">
<thead>
<tr>
<th>AccountName</th>
<th>AccountID</th>
<th>ContainerID</th>
<th>ContainerName</th>
<th>Path</th>
</tr>
</thead>
<tbody ng-repeat="(k,v) in GTMcontainersAccount">
<tr ng-if="!GTMcontainersAccount" colspan="4">
<td>No Containers found for this account</td>
</tr>
<div id="loading" name="loading" layout="row" layout-sm="column" layout-align="space-around"
ng-show="loading">
<md-progress-circular ng-show="loading" md-mode="indeterminate"></md-progress-circular>
</div>
<tr ng-repeat="(k1,v1) in v | filter:search:strict">
<td>{{ k }}</td>
<td>{{ v1.accountId }}</td>
<td ng-model="v1.containerId">{{ v1.containerId }}</td>
<td>{{ v1.name }}</td>
<td><a href="/gui/tags/{{v1.path}}">View Container</a></td>
<td>
<md-button ng-init="item.isyellow = false;" ng-class="{yellow : item.isyellow}" ng-click="item.isyellow =!item.isyellow; AddFavorite(item.isyellow,v1.containerId)" class="md-icon-button md-accent md-warn" aria-label="Favorite"">
<span ng-init="item.isyellow = false;" ng-class="{yellow : item.isyellow}" class="glyphicon glyphicon-heart"></span>
</md-button>
</td>
<td>
<md-button type="button" ng-click="row.selected=!row.selected; Hidecontainer(row.selected, v1.containerId);movePerson(index);" class="pull-right btn btn-xs">
<span ng-class="{'glyphicon':true, 'glyphicon-ok':row.selected, 'glyphicon-plus':!row.selected}"></span>
{{row.selected?'Hidden':''}}
</md-button>
</td>
</tr>
</tbody>
</table>
我在控制台中收到的错误代码:
main.min.js:3错误:[filter:notarray]预期数组但收到:0
答案 0 :(得分:0)
Angular filters适用于数组而非对象。
角度文档说
从
array
中选择项目的子集,并将其作为新数组返回。
您可以将对象转换为数组,也可以手动过滤对象,如
<div ng-repeat="(k1,v1) in v | filter:searchFn()">
<td>{{ k1 }}</td>
<td>{{ v1.accountId }}</td>
...
</div>
并在控制器中
$scope.searchFn = function(searchObject) {
var result = {};
angular.forEach(items, function(value, key) {
//your condition
});
return result;
}