从 angularjs 调用 Rest API 的最佳方法? [英] Best way to invoke Rest APIs from angularjs?

查看:27
本文介绍了从 angularjs 调用 Rest API 的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应该从哪里从 angular js 调用 REST API 请求?

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 way for it?

推荐答案

以下是我认为的最佳实践:

Here's what I would regard as best practice:

  • 使用工厂服务来处理访问 RESTful Web api 的机制
  • 将工厂服务注入需要访问的控制器
  • 在工厂中使用承诺来解决拒绝来自 $http 调用的响应
  • promise 允许控制器确定它应该如何响应 RESTful 调用的结果.例如,它可能想要调用 Toast 弹出窗口来通知用户任何问题.
  • 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.

这在数据服务层、业务逻辑和前端 UI 之间保持了良好的分离.它还限制了对任何一层或其他层的更改的任何后果.

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.

这篇关于从 angularjs 调用 Rest API 的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆