Devesh Agrawal Devesh Agrawal - 3 months ago 12
AngularJS Question

when assigning JSON data, not an array error in angularjs

This is my JSON data format.

{
"0": {
"id": "1",
"image": "/images/brands/surf_excel.png",
"name": "Surf Excel",
"productCount": "6"
},
"1": {
"id": "2",
"image": "/images/brands/rin.png",
"name": "Rin",
"productCount": "5"
},
"2": {
"id": "3",
"image": "/images/brands/ariel.png",
"name": "Ariel",
"productCount": "4"
}
}


When i am trying to assign this data like this..

$scope.Brands = [];
$scope.Brands = data; // Not an array error


Basically i want to assign data and access one by one.

How to fix this error?

SSH SSH
Answer

You can use loop for this

  angular.forEach(data,function(value,key){
   $scope.Brands.push(value);
  })
Comments