0

I have two different JSON files and they have the same attributes in them. I'm able to load them by using two promises in my service but when I go in my HTML and try to display my data they display the same thing.

This is my service:

$http.get("data.json");

            //User JSON api
            $http.get("data1.json")
                .then(function (response) {
                        dataRecievedCallback(response.data);
                    }

Should I assign my $http.get to a variable, if yes How can I do that and do I need to change anything in my controller? I haven't been coding for long and I'm new to angular so all the help is appreciated.

1 Answer 1

1
$http.get("data.json")
      .then(function (response) {
           $scope.foo = response.data;
       }
$http.get("data1.json")
      .then(function (response) {
           $scope.bar = response.data;
       }

Not sure about your "dataRecievedCallback()" function, if your function set the data into the same variable, the second $http call will overwrite the first one.

Sign up to request clarification or add additional context in comments.

3 Comments

Should I just delete my "dataRecievedCallback()"?
One more question, I dont want to elaborate my data, I want to display it as it is. Do I need to make any changes to my controller? Because I keep getting an error message "Cannot set property 'firstData' of undefined" where firstData is $scope.foo in your example.
In this case you may remove your dataReceivedCallback(), for Cannot set property 'firstData' of undefined most likely you are setting your $scope.firstData before your $http return which causing your $scope.firstData to be undefined.

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.