Eucar Eucar - 3 months ago 8
Javascript Question

Angular JS filter giving 'notArray' error

I'm getting data from my local PHP file as JSON format (here's a screenshot).

enter image description here

The filter works fine, but I'm getting this error as you can see in the pic.

core.js

var app = angular.module('cnrapp', []);

app.controller('listController', function($http) {
var vm = this;

vm.loading = true;
vm.players = {};

$http.get("api/players.php")
.then(function (res) {

var s1 = res.data.servers[0].players;
var s2 = res.data.servers[1].players;

vm.players = s1.concat(s2);
vm.loading = false;
console.log(vm.players);
});
});


index.html

<li ng-repeat="player in vm.players | filter: search">
<a href="#">{{ player.name }}</a>
</li>

Answer

You should initialize players as an array and not as an object:

vm.players = [];