in my angular app I use a factory which get json data and pass it to a controller. it is working for me if I use simple json array but fails in a nested array in case of a simple json file I have this structure
[
{
"name": "bond_1",
"profession": "Programmer",
"hometown": "St Louis, MO"
},
{
"name": "bond_2",
"profession": "Salesman",
"hometown": "Denver, CO"
},
{
"name": "bond_3",
"profession": "CEO",
"hometown": "San Francisco, CA"
}
]
my factory is this
.factory('Topology', function ($http){
var data = [];
return{
get: function(){
if (data.length == 0){
$http.get("data.json")
.success(function (response){
for(var i=0, ii=response.length; i<ii; i++){
data.push(response [i]);
}
});
}
return data;
},
}
});
and my controller is this
var installerControllers =angular.module('installerControllers', []);
installerControllers.controller('stageThreeCtrl', function ($scope, Topology) {
$scope.bonds=Topology.get();
})
now it all working fine and I can view the data when I doing ng-repeat on it from the view
but i need instead of the simple json structure use a nested array which looks like this
{
"bonds":[
{
"name": "Alex",
"profession": "Programmer",
"hometown": "St Louis, MO"
},
{
"name": "David",
"profession": "Salesman",
"hometown": "Denver, CO"
},
{
"name": "Laura",
"profession": "CEO",
"hometown": "San Francisco, CA"
}
],
"networks":[
{
"name": "test",
"all_hosts": "false",
"IP_Version": "IPV4",
"IP address": "10.10.10.10",
"IPV net mask": "255.255.255.0",
"Interface": "bond 0",
"VLAN TAG": "4001",
"Description": "some custom description"
}
]
}
now I am trying to call for one of the objects from the controller in this way
var installerControllers =angular.module('installerControllers', []);
installerControllers.controller('stageThreeCtrl', function ($scope, Topology) {
var data=Topology.get();
$scope.bonds=data.bonds;
})
but it is not working and I got in the console.log an empty array
your help will be very appreciated
$q
into that factory and use that to project out the data.var data = [];
should bevar data = {};
, and you shouldn't use.push
, instead use assignmentdata[key] = response[key]