如何在 angularjs 中使用多个索引页
How to use multiple index pages in angularjs
我想访问我认为的 localhost:3000/admin ..
index.html 和 admin.html 是我的两个不同的基本文件,一个用于用户,另一个用于管理仪表板
在我的 app.routes.js 我有这个
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 我有这个
app.get('*', function(req, res){
res.sendFile(__dirname + '/public/app/views/index.html');
});
有两个 html 索引文件:index.html、admin.html
我想在 url 是:localhost:3000/admin
时打开 admin.html
和index.html当url是:localhost:3000
screenshot of the app structure
在 views/pages 我有 index.html 和 admin.html 需要的所有 html 页面使用 ng-view
据我了解你的问题,你可能只想再添加一个路由规则来表达。 Express 使用第一个匹配的路由,所以这里的顺序很重要。
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 重写为单个应用程序入口点: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
明确一点:我建议您为两个单独的 应用程序创建服务器路由。没有什么能阻止你在另一条路线上再次使用第一个应用程序,但我建议不要这样做。将后端真正强大的功能与 public 应用分开。 IE。从您的 ng-routes 中删除 /admin/login
和 /admin
并为此创建一个单独的应用程序。
我找到了更好的解决方案..
app.get('*', function(req, res){
if (req.url.match('^\/admin')) {
res.sendFile(__dirname + '/public/app/views/admin.html');
} else {
res.sendFile(__dirname + '/public/app/views/index.html');
}
});
我想访问我认为的 localhost:3000/admin .. index.html 和 admin.html 是我的两个不同的基本文件,一个用于用户,另一个用于管理仪表板
在我的 app.routes.js 我有这个
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 我有这个
app.get('*', function(req, res){
res.sendFile(__dirname + '/public/app/views/index.html');
});
有两个 html 索引文件:index.html、admin.html 我想在 url 是:localhost:3000/admin
时打开 admin.html和index.html当url是:localhost:3000
screenshot of the app structure
在 views/pages 我有 index.html 和 admin.html 需要的所有 html 页面使用 ng-view
据我了解你的问题,你可能只想再添加一个路由规则来表达。 Express 使用第一个匹配的路由,所以这里的顺序很重要。
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 重写为单个应用程序入口点: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
明确一点:我建议您为两个单独的 应用程序创建服务器路由。没有什么能阻止你在另一条路线上再次使用第一个应用程序,但我建议不要这样做。将后端真正强大的功能与 public 应用分开。 IE。从您的 ng-routes 中删除 /admin/login
和 /admin
并为此创建一个单独的应用程序。
我找到了更好的解决方案..
app.get('*', function(req, res){
if (req.url.match('^\/admin')) {
res.sendFile(__dirname + '/public/app/views/admin.html');
} else {
res.sendFile(__dirname + '/public/app/views/index.html');
}
});