0

I have various page in my project and each page contain lots of drop down.

For getting options of drop-down from web-api and then i store this variable in angular service variable for further use in another view.

But i didn't get the way where should i call web-api to get drop-down data so that it doesn't matter which page should i open first or by default???

And i get options of drop-down every where.Currently i calling web-api in the controller which is correspond to my first view of application.

My approaches regarding this: Method 1: I create a root controller in which i call angular service which call web-api to get data. The view is already render on view-port before get the the from web-api.

Method 2: i call angular service which call web-api in controller(not root controller which to specific to a view) and populate data in drop-down after successful callback from service so this working fine but it's not generic

Rishabh Garg
  • 706
  • 1
  • 9
  • 28

1 Answers1

0

Services/Factory are singleton, so I believe you should call the web api in your service rather than in controller and then passing the data to service.

EDIT -

If you are concern about the display of view before the data then you can use resolve as resolve can wait for data to become available before showing a view.

swapnesh
  • 26,318
  • 22
  • 94
  • 126
  • i calling web-api from services and controller get data from service and i locally store these drop-down data in angular services(service name sharedProperties) variable to further access. but i suffer from that my view load on view-port before getting data from web-api and if call service in particular controller specific to first view it's not generic. – Rishabh Garg Jul 30 '15 at 17:29
  • @grishabh can you please clarify - "but i suffer from that my view load on view-port before getting data from web-api and if call service in particular controller specific to first view it's not generic" – swapnesh Jul 30 '15 at 17:51
  • Method 1: I create a root controller in which i call angular service which call web-api to get data. The view is already render on view-port before get the the from web-api. Method 2: i call angular service which call web-api in controller(not root controller which to specific to a view) and populate data in drop-down after successful callback from service so this working fine but it's not generic. – Rishabh Garg Jul 30 '15 at 18:19
  • @grishabh what about using resolve as you can wait for data to become available before showing a view. – swapnesh Jul 30 '15 at 18:24
  • yes already used that. I get data in root controller and it in some variable, But my view is using that variable to get data, and this time data is empty because service not responded yet. so i want a generic approach to solve this so that i can use this drop-down every where. is any functionality that execute before controller. I already use the angular.run utility. – Rishabh Garg Jul 30 '15 at 18:30