【问题标题】:redirect to different route when root url changes根 url 更改时重定向到不同的路由
【发布时间】:2014-04-20 14:24:50
【问题描述】:

我正在开发一个 anuglar + nodejs 应用程序并遇到了这个问题。我想设置以下url映射:

  1. localhost:3000/ => 节点服务器在服务器端提供 index.html,然后在客户端提供部分视图 index.html(有两个 index.html 一个在服务器端一个客户端) localhost:3000/signup => angular routeProvider 将部分视图 signup.html 服务到 index.html(ajax 所以无需重新加载)

  2. localhost:3000/customerLoggedin =>节点服务器为 customerLoggedin.html 服务,也许 Angular 服务于其他部分(页面重新加载) localhost:3000/customerLoggedin/order-form => angular routeProvider 提供部分视图 order.html (ajax)

我有以下代码:

角边:

system.js:

angular.module('myApp.system').config(['$stateProvider', '$urlRouterProvider','$locationProvider',
        function($stateProvider, $urlRouterProvider,$locationProvider) {
            // For unmatched routes:
            $urlRouterProvider.otherwise('/');
            // states for my app
            $stateProvider.state('home', {
                    url: '/',
                    templateUrl: 'public/system/views/partials/index.html' // there are two index.html one on the server side (which is the layout) and one served by angular
                }).state('signup',{
                url:'/signup',
                    templateUrl:'public/system/views/partials/signup.html'
                }).state('payment',{
                    url:'/payment',
                    templateUrl:'public/system/views/partials/payment.html',
                    controller:'paymentCtrl'
                }).state('order',{
                    url:'/order-form',
                    templateUrl:'public/system/views/partials/order.html',
                    controller:'orderCtrl'
                });
        }
    ])

customerLoggedin.html:

   ...
   <a href='/order-form'><span>New Order</span></a>
   ...
   <section data-ui-view ng-view class='view-animate'/>
   ...

节点服务器上的路由:

module.exports = function(app){
    app.get('/customerLoggedIn',function(req,res){
        res.render('customerLoggedIn');
        });

        app.get('/',function(req,res){
        res.render('index');
        });

};

现在的问题是,当我点击 customerLoggedin.html 中的“新订单”链接时,角度给了我 client-sdie index.html,它应该只显示在 localhost:3000/index.html 中。我希望 Angular 将部分视图 order.html 提供给 customerLoggedin.html 中的 ngview

如何做到这一点?

提前致谢

【问题讨论】:

    标签: javascript html node.js angularjs


    【解决方案1】:

    尝试ui-sref指令触发状态改变,而不是指向url

    <a ui-sref='order'>New Order</a>
    

    【讨论】:

    • 这可能有效,但是当浏览器第一次加载localhost:3000/customerLoggedIn angular 认为它是“/”并加载 index.html 时,您如何解决问题?
    • 如果 localhost:3000/customerLoggedIn 是一个不同的渲染 html,它本质上是一个具有不同状态集的不同角度应用程序。您的替代方法是将 /customerLoggedIn 作为从 index.html 提供的主要 Angular 应用程序中的另一条路线
    猜你喜欢
    • 1970-01-01
    • 2018-01-12
    • 2013-11-27
    • 2013-07-23
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 2011-12-07
    相关资源
    最近更新 更多