0

In my angularjs service I want an array to be filled as follows:

var results = [
  {id: 1, name: 'John'},
  {id: 2, name: 'Jane}
]

I want to do this by calling an API. So far I have written this:

function getDataFromAPI() {
  $http.get('foo.com/bar.json').then(function(data){
    console.log( data.data.employees);
  });
}

var results = getDataFromAPI();

When I call results, in my chrome dev tools it will display the results from the json file, but I don't know how to populate the results array.

How can I fill the array?

1 Answer 1

1

$http.get (and many other functions in Angular) returns a promise.

var results;
function getDataFromAPI() {
    return $http.get('foo.com/bar.json');
}

getDAtaFromAPI().then(function(data) {
    results = data;
});
Sign up to request clarification or add additional context in comments.

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.