AngularJS ngRoute direct part access

醉酒当歌 提交于 2019-12-12 06:13:18

问题


My app works fine with ngRoute, but when I'm trying to access my parts directly(from address bar), it returns just the part (without main page). How to hide these parts from a direct access?

Explanation: I have a site with 2 blocks. First is a menu and second is a content (one of my route's part). Menu has links: "#/main" and "#/account". When I press on the buttons, it works fine (left menu and content). But if I change URL from localhost:8080/#/account to localhost:8080/account, it renders ONLY content, without menu. I want to hide access to localhost:8080/account or make it to render a content with a menu.


回答1:


Your problem is most likely not with your AngularJS routing, but the routing from the server. When you request a page like localhost:8080/account, your server says "ok, let's JUST deliver the /account file". But that's not quite right, because you actually want the whole app to load. It's a common problem, and not too bad to solve.

I don't know what your backend looks like, but here's a generic example with express/node:

var express = require('express'),
  routes = require('./routes');

app.get('/', routes.index);
app.get('*', routes.index);

"Every request to the backend should initially render the complete layout in order to load our Angular app. Only then will the client-side rendering take over."

src: http://fdietz.github.io/recipes-with-angular-js/backend-integration-with-node-express/implementing-client-side-routing.html



来源:https://stackoverflow.com/questions/31975774/angularjs-ngroute-direct-part-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!