javascript - How do I load multiple json arrays based on ng-route? -
i'm new angular , wondering if there way can load multiple json arrays app. basically, have json document contains data looks kinda example.
it has multiple arrays within same file. thinking maybe loop through them , load data, i'm not sure.
{ 'title': 'myapp', 'data1': [ { 'listname': 'name1', 'id': '1' }, { 'listname': 'name2', 'id': '2' }, ], 'data2': [ { 'listname': 'name1', 'id': '1' }, { 'listname': 'name2', 'id': '2' }, ], 'data3': [ { 'listname': 'name1', 'id': '1' }, { 'listname': 'name2', 'id': '2' }, ] }
i found service load data this.
app.factory('grabdata', ['$http', function($http) { return { get: function(callback) { $http.get('data/names.json').success(function(data) { callback(data); }); } } }]);
my controller looks below , don't want load each data this. there way can switch arrays based on route instead of loading 1 one? can see below, not best way doing it.
grabdata.get(function(data){ $scope.thedata = data; $scope.mytotal1 = data.data2.length; $scope.mylist1 = data.chapter1; $scope.mytotal2 = data.data2.length; $scope.mylist2 = data.data2; $scope.mytotal3 = data.data3.length; $scope.mylist3 = data.data3; $scope.mytotal4 = data.data4.length; $scope.mylist4 = data.data4; });
you see going. data5, data6, etc.
i have routes set , if somehow load them based on routes have, work good, suppose.
Comments
Post a Comment