AngularJS可以在没有REST API的情况下使用吗?

时间:2021-07-07 08:20:24

When I am creating a simple website with node.js I am fine with using the view engine (eg. jade) and controllers that provide data to it (eg. simple todo list). However, if I decide to add AngularJS as the client framework then it seems that I must implement REST API on the backend to get data from it. Almost all examples I see online with AngularJS have this basic architecture: client (angular) communicates with the server via REST API.

当我使用node.js创建一个简单的网站时,我可以使用视图引擎(例如,jade)和为其提供数据的控制器(例如,简单的待办事项列表)。但是,如果我决定将AngularJS添加为客户端框架,那么我似乎必须在后端实现REST API以从中获取数据。几乎所有我在线使用AngularJS看到的例子都有这个基本架构:client(angular)通过REST API与服务器通信。

Can AngularJS be used without REST API and if so should I do it or should avoid it? Are there any recommendation/best practices for using AngularJS without REST API backend?

可以在没有REST API的情况下使用AngularJS,如果是这样,我应该这样做还是应该避免使用它?在没有REST API后端的情况下使用AngularJS是否有任何建议/最佳实践?

4 个解决方案

#1


6  

Absolutely. Angular can still do a lot on your site even if you never utilize the $http service to talk to your server. You can still take advantage of the utilities for helping out with managing your DOM.

绝对。即使您从未使用$ http服务与您的服务器通信,Angular仍然可以在您的网站上做很多事情。您仍然可以利用这些实用程序来帮助管理DOM。

That said, most modern apps need to get data from the server. There are tons of reasons why you might need to do this. For example, if you had users that needed to sign up then you'd need to store their username and password somewhere. That somewhere would be in a database that only your server can access. Then your server would provide some URLs that you can talk to via Angular's $http service.

也就是说,大多数现代应用需要从服务器获取数据。您可能需要这么做的原因很多。例如,如果您有需要注册的用户,那么您需要在某处存储他们的用户名和密码。那个地方将在只有你的服务器才能访问的数据库中。然后您的服务器将提供一些您可以通过Angular的$ http服务与之交谈的URL。

If you do have an app that makes calls to the server but you want to turn off the network communication for testing, you can mock the $http call responses. Angular provides an $httpBackend for that exact purpose. You can use it to set up dummy URLs that pretend to respond to your $http calls so that your $http calls don't know they aren't actually talking to a server.

如果您确实有一个应用程序可以调用服务器,但您想关闭网络通信以进行测试,则可以模拟$ http调用响应。 Angular为此目的提供$ httpBackend。您可以使用它来设置虚假URL,假装响应您的$ http调用,以便您的$ http调用不知道他们实际上没有与服务器通话。

authRequestHandler = $httpBackend.when('GET', '/auth.py')
                       .respond({userId: 'userX'}, {'A-Token': 'xxx'});

Perfect for testing your code without a REST backend during testing.

非常适合在测试期间无需REST后端测试代码。

#2


1  

REST which is short for Representational state transfer is basically things or resources instead of actions. Yes AngularJS can be used without REST API.

REST是Representational状态转移的缩写,基本上是事物或资源而不是动作。是的,AngularJS可以在没有REST API的情况下使用。

#3


1  

You can use nodeJS for your restful API and AngularJS as your javascript framework.

您可以将nodeJS用作restful API,将AngularJS用作javascript框架。

Even without a restful API AnguarlJS is a very strong tool to use in a project although to use it to it's full potential (fully scaled web app) then you would need a restful API.

即使没有一个安静的API,AnguarlJS也是一个非常强大的工具,可以在一个项目中使用,尽管它可以充分利用它(完全扩展的Web应用程序),那么你需要一个安静的API。

#4


0  

use $http for not RESTful API use $resource for RESTful API

使用$ http而不是RESTful API使用$ resource用于RESTful API

#1


6  

Absolutely. Angular can still do a lot on your site even if you never utilize the $http service to talk to your server. You can still take advantage of the utilities for helping out with managing your DOM.

绝对。即使您从未使用$ http服务与您的服务器通信,Angular仍然可以在您的网站上做很多事情。您仍然可以利用这些实用程序来帮助管理DOM。

That said, most modern apps need to get data from the server. There are tons of reasons why you might need to do this. For example, if you had users that needed to sign up then you'd need to store their username and password somewhere. That somewhere would be in a database that only your server can access. Then your server would provide some URLs that you can talk to via Angular's $http service.

也就是说,大多数现代应用需要从服务器获取数据。您可能需要这么做的原因很多。例如,如果您有需要注册的用户,那么您需要在某处存储他们的用户名和密码。那个地方将在只有你的服务器才能访问的数据库中。然后您的服务器将提供一些您可以通过Angular的$ http服务与之交谈的URL。

If you do have an app that makes calls to the server but you want to turn off the network communication for testing, you can mock the $http call responses. Angular provides an $httpBackend for that exact purpose. You can use it to set up dummy URLs that pretend to respond to your $http calls so that your $http calls don't know they aren't actually talking to a server.

如果您确实有一个应用程序可以调用服务器,但您想关闭网络通信以进行测试,则可以模拟$ http调用响应。 Angular为此目的提供$ httpBackend。您可以使用它来设置虚假URL,假装响应您的$ http调用,以便您的$ http调用不知道他们实际上没有与服务器通话。

authRequestHandler = $httpBackend.when('GET', '/auth.py')
                       .respond({userId: 'userX'}, {'A-Token': 'xxx'});

Perfect for testing your code without a REST backend during testing.

非常适合在测试期间无需REST后端测试代码。

#2


1  

REST which is short for Representational state transfer is basically things or resources instead of actions. Yes AngularJS can be used without REST API.

REST是Representational状态转移的缩写,基本上是事物或资源而不是动作。是的,AngularJS可以在没有REST API的情况下使用。

#3


1  

You can use nodeJS for your restful API and AngularJS as your javascript framework.

您可以将nodeJS用作restful API,将AngularJS用作javascript框架。

Even without a restful API AnguarlJS is a very strong tool to use in a project although to use it to it's full potential (fully scaled web app) then you would need a restful API.

即使没有一个安静的API,AnguarlJS也是一个非常强大的工具,可以在一个项目中使用,尽管它可以充分利用它(完全扩展的Web应用程序),那么你需要一个安静的API。

#4


0  

use $http for not RESTful API use $resource for RESTful API

使用$ http而不是RESTful API使用$ resource用于RESTful API