简体   繁体   中英

AngularJS ng-repeat | Track by $index wont work

I cannot get track by $index to work on my ng-repeat table. I get error that says "Expected array but recieved:0". The problem is that my JSON dont have any ID so i need to create some ID for each row.

Controller:

function getContainer() {
     $http.get("/api/containers")
        .then(function (response) {
              $scope.GTMcontainersAccount = response.data;
              $scope.loading = false;
       })
}

$scope.GTMcontainersAccount contains (JSON):

{
    "0": [],
    "Account2": [{
        "accountId": "1746****",
        "containerId": "745****",
    }, {
        "accountId": "1746****",
        "containerId": "751****",
    }],
    "Account 1": [{
        "accountId": "17661****",
        "containerId": "748****",
    }]
}

View:

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

Error code that i get in console:

main.min.js:3 Error: [filter:notarray] Expected array but received: 0

Angular filters works on arrays and not objects.

angular documentation says

Selects a subset of items from array and returns it as a new array.

Either you can convert the object into an array or you can filter the object manually like

<div ng-repeat="(k1,v1) in v | filter:searchFn()">
    <td>{{ k1 }}</td>
    <td>{{ v1.accountId }}</td>
    ...
</div> 

and in controller

$scope.searchFn = function(searchObject) {
    var result = {};
    angular.forEach(items, function(value, key) {
        //your condition
    });
    return result;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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