如何在angularjs中使用多个索引页 [英] How to use multiple index pages in angularjs
问题描述
我想访问我认为的 localhost:3000/admin ..index.html 和 admin.html 是我的两个不同的基本文件,一个用于用户,另一个用于管理仪表板
I want to access the localhost:3000/admin which is in my views .. the index.html and the admin.html are my two different base files one is for users and the other is for admin dashboard respectively
在我的 app.routes.js 我有这个
in my app.routes.js I have this
angular.module('appRoutes', ['ngRoute'])
.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/', {
templateUrl: 'app/views/pages/home.html',
controller: 'MainController',
controllerAs: 'main'
})
.when('/admin', {
templateUrl: 'app/views/admin.html',
})
.when('/logout', {
templateUrl: 'app/views/pages/home.html'
})
.when('/login', {
templateUrl: 'app/views/pages/login.html'
})
.when('/signup', {
templateUrl: 'app/views/pages/signup.html'
})
.when('/admin/login' ,{
templateUrl: 'app/views/pages/adminlogin.html'
})
$locationProvider.html5Mode(true);
})
在 server.js 我有这个
in server.js I have this
app.get('*', function(req, res){
res.sendFile(__dirname + '/public/app/views/index.html');
});
有两个html索引文件:index.html、admin.html我想打开 admin.html 当 url 是:localhost:3000/admin
there are two html index files: index.html, admin.html i want to open admin.html when the url is: localhost:3000/admin
和 index.html 当 url 是:localhost:3000
and index.html when the url is: localhost:3000
在视图/页面中,我拥有使用 ng-view 的 index.html 和 admin.html 所需的所有 html 页面
in views/pages I have all the html pages required by index.html and admin.html using ng-view
推荐答案
据我了解您的问题,您可能只想再添加一个路由规则来表达.Express 使用匹配的第一条路线,因此这里的顺序很重要.
As I understand your question, you would want to just add one more routing rule to express. Express uses the first route that matches, so order is important here.
app.get('/admin/*', function(req, res){
res.sendFile(__dirname + '/public/app/views/admin.html');
});
app.get('*', function(req, res){
res.sendFile(__dirname + '/public/app/views/index.html');
});
关于 html5mode 的 angular 文档说明您应该将所有 url 重写为单个应用程序入口点:使用此模式需要在服务器端重写 URL,基本上您必须将所有链接重写为应用程序入口点(例如 index.html).
https://docs.angularjs.org/guide/$location#server-side一个>
The angular docs state about html5mode that you should rewrite all your urls to a single app entry point: Using this mode requires URL rewriting on server side, basically you have to rewrite all your links to entry point of your application (e.g. index.html).
https://docs.angularjs.org/guide/$location#server-side
所以要明确:我建议您为两个独立的应用程序创建服务器路由.没有什么可以阻止您在另一条路线上再次使用第一个应用程序,但我建议您不要这样做.将任何对您的后端具有真正权力的东西与公共应用程序分开.IE.从您的 ng-routes 中删除 /admin/login
和 /admin
并为此创建一个单独的应用程序.
So to be clear: What I suggest is that you create server-routes for two separate apps. Nothing prevents you from using the first app one more time on another route, but I would advice against it. Separate anything with real power over your backend from the public app. I.e. remove /admin/login
and /admin
from your ng-routes and simply create a separate app for that.
这篇关于如何在angularjs中使用多个索引页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!