AngularJS 路由与后端路由 [英] AngularJS routing vs backend routing

查看:23
本文介绍了AngularJS 路由与后端路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的下一个项目中使用 AngularJS.具有 Python 后端和 html5、Angular 前端的应用程序.

I would like to use AngularJS in my next project. The application with Python backend and html5, Angular frontend.

我将在后端使用 MVC 框架,我有点困惑.我必须在后端和前端使用路由吗?

I am going to use MVC framework on backend and I am little bit confused. Do I have to use routing on backend and also frontend?

因为我一直使用后端路由,而前端路由对我来说真的是一个新想法.

Because I always used backend routing and routing on frontend is really new idea for me.

客户端路由更好吗?而当我选择使用前端路由时,后端不会有路由吗?所有请求都将发送到一个 url?

Is client side routing better? And when I choose to use frontend routing, there will be no routes on backend? All request will be send to one url?

推荐答案

可以使用客户端路由,让后端返回静态文件和JSON数据.Angular 中的路由基本上告诉您需要从服务器下载哪个部分模板以及哪个控制器将处理它.

You can use client-side routing and let the backend return static files and JSON data. The routing in Angular basically tell you which partial template you need to download from server and which controller will handler it.

你的后端路由会是这样的

Your back end routes will be like this

'/partials/:name' -> return corresponding partial

'/api/*' -> handlers to return json data

'/*' -> return index.html

您的 index.html 将包含对其他视图的引用

Your index.html will contain reference to other views

...
<div ng-view></div>
...

现在假设您访问 yourapp.com/someview.html.服务器返回index.html 并且由于 url 是/someview,Angular 将向服务器请求someview"部分并相应地呈现页面.

Now let say you go to yourapp.com/someview.html. The server returns index.html and since the url is /someview, Angular will ask for "someview" partial from server and render the page accordingly.

简而言之,服务器的作用是返回 index.html、partials 和服务 REST API 请求.客户端始终接收 index.html 并根据 url 请求对应的部分和 JSON 数据.

In short, the server role is to return index.html, partials and serve REST API request. The client always receive index.html and based on the url, request for coressponding partials and JSON data.

这篇关于AngularJS 路由与后端路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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