0

In my web app, I would like to load all of the user data at the beginning, in the run method and pass the data to the other controllers to be shown to the user in the dashboard.

So lets say I have a data structure on the server

data = {
  x : 30,
  y: 20
}

I would like to get it in one http requests (instead of creating multiple http requests and creating multiple promises)

The http request I run in the run method of the app

service.getDataFromServer = function(){
  return $http.get('/admin/data').then(function(response) {
    data = response.data;
  });
};

This will create a promise.

I would like to take this promise and pass it to other controllers to be used.

My first question is how can I do it so the data from the promise will bind to the data I am about to show to the user.

The second question is, can I use the structure fields before the promise is even resolved? for example

$scope.x = data.x

And by data I mean the data which is about to be resolved

Thanks

1 Answers1

0

I would like to get it in one http requests (instead of creating multiple http requests and creating multiple promises)

After getting response from http call,save the data in factory/services so,you can use is at another location like controller in your case by injecting it which is most recommended.

I would like to take this promise and pass it to other controllers to be used. how can I do it so the data from the promise will bind to the data I am about to show to the user.

make http call and save the promise.

service.getDataFromServer = function() {

      var promise = $http.get('test.json');

      service.savePromise(promise);
    }

and

get saved promise.

.controller('testCtrl', function(service) {

      service.getSavedPromise().then(function(response){
          console.log(response.data)
      })
  })

Plunker here

The second question is, can I use the structure fields before the promise is even resolved? for example

$scope.x = data.x  

// data is still undefined as promise is not resolved.so,you will get TypeError

What you can do best here,you can merge data to your existing data object/array,so,Angular two way binding can do rest of the things for you.

RIYAJ KHAN
  • 15,032
  • 5
  • 31
  • 53
  • Hey Ritchie, Thanks a lot. Can you please tell me what you mean by "What you can do best here,you can merge data to your existing data object/array,so,Angular two way binding can do rest of the things for you." do you mean to create the structure in angular and in the then function, to set each field I get from the server to its corresponding field in the structure? – Assaf asdasdas Mar 29 '17 at 07:25
  • And another thing, I can use the then function multiple times in multiple controllers right? – Assaf asdasdas Mar 29 '17 at 07:27
  • yes,you can use in multiple controller. Best things means,your object structure should be consistent on Front end and backend so,you can easily add and merge the new object response from backend to Front end – RIYAJ KHAN Mar 29 '17 at 07:55