问题
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