如何在angularjs中使用多个索引页面 [英] How to use multiple index pages in angularjs

查看:97
本文介绍了如何在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
i想要打开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

应用结构截图

在views / pages中我拥有index.html和admin.html所需的所有html页面使用ng-view

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的角度文档说明您应该将所有网址重写为单个应用入口点:使用此模式需要在服务器端重写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

所以要明确:我建议您为两个单独的应用创建服务器路由。没有什么能阻止你在另一条路线上再次使用第一个应用程序,但我会反对它。通过公共应用程序在后端分离任何具有真正威力的东西。即从您的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屋!

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