简体   繁体   English

AngularJS:首次加载页面时使用给定URL的路由

[英]AngularJS: Use route for given URL when loading page first time

I develop SPA using AngularJS. 我使用AngularJS开发SPA。 My config looks like this: 我的配置如下所示:

app.config(["$routeProvider",
    function($routeProvider) {
        return $routeProvider
            .when("/", {
                redirectTo: "/clients"
            }).when("/clients", {
                templateUrl: "app/views/crm/clients/list.html"
            }).when("/client/:id?", {
                templateUrl: "app/views/crm/clients/edit.html"
            }).when("/404", {
                templateUrl: "app/views/pages/404.html"
            }).otherwise({
                redirectTo: "/404"
            });
    }
]);

I would like to let my users share URLs for clients: http://myapp.com/#/client/1 , http://myapp.com/#/client/2 etc. The idea is that user writes URL to location bar and gets to client page for a specific client. 我想让我的用户共享客户端的URL: http://myapp.com/#/client/1 : http://myapp.com/#/client/1 : http://myapp.com/#/client/2等。这个想法是用户将URL写入位置栏并转到特定客户的客户页面。

When I tried to listen for $routeChangeStart event I found out that current parameter in event callback is empty when page is loaded for first time and next.$$route.originalPath is always / . 当我尝试监听$routeChangeStart事件时,我发现在第一次和next.$$route.originalPath一次首次加载页面时,事件回调中的current参数为空next.$$route.originalPath始终为/

$rootScope.$on('$routeChangeStart', function(event, next, current) {
      console.log(current);                   // undefined
      console.log(next.$$route.originalPath); // /
      console.log(next.$$route.redirectTo);   // /clients
});

So how can I get the original URL sent to server to use route requested? 那么如何获取发送到服务器的原始URL以使用请求的路由?

UPDATE UPDATE

As it turned out, the problem was in my own redirect that was triggered when user session token from cookies was extracted. 事实证明,问题出在我自己的重定向中,该重定向是从Cookie中提取用户会话令牌时触发的。 It redirected to / every user who entered application with session cookie. 它重定向到/谁进入与会话cookie应用程序的每个用户。

You can use 您可以使用

document.URL  

or 要么

$window.location

ie: 即:

 $rootScope.$on('$routeChangeStart', function(event, next, current) {
     console.log(document.URL);   
     console.log($window.location)       

});

Please see demo here 请在这里查看演示
http://plnkr.co/edit/oaW40vWewxdZYxRkCW8c?p=preview http://plnkr.co/edit/oaW40vWewxdZYxRkCW8c?p=preview

Finally I came up with solution which looks like dirty hack yet works. 最后,我想出了一个解决方案,看起来像是肮脏的骇客,但仍然有效。

var app = angular.module('app', [ ... ]);

app.run(function (...) {

    ...

    var firstTimeLocationChanged = true;
    $rootScope.$on('$locationChangeStart', function(event, next, current) {
        var savedPath = current.split('#').slice(-1);
        if (firstTimeLocationChanged && ['/', '/login'].indexOf(savedPath) < 0) {
            firstTimeLocationChanged = false;
            setTimeout(function() {
                console.log('redirect to: ' + savedPath);
                $location.path(savedPath);
            }, 3000);
        }
    });

    ...

});

UPDATE UPDATE

As it turned out, the problem was in my own redirect that was triggered when user session token from cookies was extracted. 事实证明,问题出在我自己的重定向中,该重定向是从Cookie中提取用户会话令牌时触发的。 It redirected to / every user who entered application with session cookie. 它重定向到/谁进入与会话cookie应用程序的每个用户。 So the solution above is not needed at all. 因此,根本不需要上述解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM