1

I have 3 different json files chart1.json, chart2.json, chart3.json. All the jsons have different data structure and one of the key that is required from the jsons is "timestamps","fieldname" and "statename" respectively and need to be put into a varibale called "category" which will be viewed on html for 3 diffrent charts.Like 3 diffrent charts will have 3 categories "timestamp",fieldname" and "statename". I need to do this with one chartcontroller.

Is making 3 diffrent calls to these json files is the only way??

1
  • Share your code please ? Commented Aug 20, 2015 at 5:40

3 Answers 3

2

SuperVeetz wouldnt it be nicer to put those calls in a resolve function of the view?

resolve

resolve: {
            'init':function (initService) {
                return initService.init();
            }

where inititService holds the requests:

module.factory('initService', function($q, $http) {
            var request = $q.defer();
            return {
                init: function(option) {
                        chart1 = $http.get('path/to/json/');
                        chart2 = $http.get('path/to/json/');
                        chart3 = $http.get('path/to/json/');
                        $q.all([chart1, chart2, chart3])
                            .then(function(responses) {
                                    request.resolve(responses);
                                }
                        }

}

ctrl:

module.controller('ChartCtrl', ['$scope', 'init', function($scope, init) {
    $scope.someCharts = init;
   }]);
Sign up to request clarification or add additional context in comments.

2 Comments

I was always confused when to use $resolve or $http. can you provide me any reference where I can understand the real diffrence
the resolve function will be called before your view is rendered/controller runs. so if you want to work with the data you should load them in your resolve function. So that your controller will only start if the data is fetched. inside your controller the data from your resolve function is available with the name you provided in the resolve object. here it is "init".
0
  var model = {};

  angular.module('yourApp', [])

  .run(function($http) {

    $http.get("chart1.json").success(function(data) {
      model.chart1 = data;
    });

    $http.get("chart2.json").success(function(data) {
      model.chart2 = data;
    });

    $http.get("chart3.json").success(function(data) {
      model.chart3 = data;
    });

  })

  .controller('ChartCtrl', ['$scope', '$http', function($scope, $http) {

    $scope.someCharts = model;

  }]);

pretty sure that 3 http responses is the only way, inless you have a server and database that can parse the JSON for you or somethin

Comments

0

If you want to make one call, you would need to create a function on the server which combines (re-structures) your three JSON files into one as soon as you request it with a specific api call.

$http.get('/api/getThreeInOne').then(function(data){ return data;}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.