Best way to invoke Rest APIs from angularjs?

后端 未结 6 406
遇见更好的自我
遇见更好的自我 2020-12-28 18:44

From where should REST API request be invoked from angular js?

I.e. from controller, module, service, factory. I am totally confused that what should be the right w

6条回答
  •  情话喂你
    2020-12-28 19:19

    Here's what I would regard as best practice:

    • Use a factory service to handle the mechanics of access to the RESTful web api
    • Inject the factory service into the controller that needs access
    • Use a promise in the factory to resolve or reject the response from the $http call
    • The promise allows the controller to determine how it should respond to the result of the RESTful call. For example, it may want to invoke a toast popup to notify the user of any problem.

    This maintains good separation between the data service layer, the business logic and the front-facing UI. It also limits any consequences of changes to any one layer, to other layers.

提交回复
热议问题