0

How can I get the data from two JSON files, named gridData.json and AddedData.json simultaneously using $http.get function?

PApp.controller('ProjectDataController', function($scope, $http) {
    $scope.addProject=function($scope){

    };
    $scope.getData = function(){
        $http.get('AddedProjects.json').success(function(data) {
            $scope.ProjectStat = data;
        });
        $http.get('JSON/gridData.json').success(function(data) {
            $scope.ProjectStat = data;
        });
    };
});
3
  • Please check. The code of the controller is given. Commented Feb 24, 2015 at 11:27
  • your code seems correct. only issue is that it uses the same scope variable. meaning one will overwrite the other Commented Feb 24, 2015 at 11:29
  • You might want to merge the objects... Commented Feb 24, 2015 at 11:30

1 Answer 1

1

Better is to use two seperate variables on the scope. But if you have your reasons to keep it the same objects. You can merge them like below.

PApp.controller('ProjectDataController', function($scope, $http) {
    $scope.ProjectStat = {};
    $scope.addProject=function($scope){

    };
    $scope.getData = function(){
        $http.get('AddedProjects.json').success(function(data) {
            angular.extend($scope.ProjectStat, data);
        });
        $http.get('JSON/gridData.json').success(function(data) {
            angular.extend($scope.ProjectStat, data);
        });
    };
});
Sign up to request clarification or add additional context in comments.

4 Comments

This code is giving me an error. Error: dst is undefined extend@localhost:8080/ProjectCalendar/js/angular.js:406:7 $scope.getData/<@localhost:8080/ProjectCalendar/js/index.js:19:4 $http/promise.success/<@localhost:8080/ProjectCalendar/js/angu
check what is getting returned in data in both the cases.
I have solved it. By taking two separate $scope variables. I will let you know the return values.
On the clock of the button, the error is displayed. The function isn't even getting called.

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.