如何从AngularJS控制器重定向到Express路线?

时间:2022-04-12 06:36:28

I'm using the boilerplate stack from MEAN.io and finding it quite a good starting point, however I'm having trouble mixing the different routing commands. My app is going to have a simple signin page which is public, everything else is hidden behind that. I can check if the user is authenticated no problem, but I cannot for the life of me get Angular to load the signin page from the server. I already have a signin button on my html page that calls the correct route no problem at all, I just can't do the same thing from code.

我正在使用MEAN.io的样板堆栈,并发现它是一个很好的起点,但是我在混合不同的路由命令时遇到了麻烦。我的应用程序将有一个简单的登录页面是公开的,其他一切都隐藏在其后面。我可以检查用户是否经过身份验证没有问题,但我不能为我的生活让Angular从服务器加载登录页面。我的html页面上已经有一个signin按钮,调用正确的路径完全没有问题,我只是无法从代码中做同样的事情。

The $location.path('/signin'); code doesn't call the server because it leaves the hash in the path

$ location.path('/ signin');代码不会调用服务器,因为它会在路径中留下哈希值

My Angular controller

我的角度控制器

angular.module('tms.tweets').controller('TweetsController', ['$scope', '$routeParams',
'$location', '$resource', 'Global', 'Tweets', function ($scope, $routeParams, $location,
$resource, Global, Tweets) {
$scope.global = Global;

$scope.find = function() {

    if(Global.authenticated){

        console.log(Global.authenticated);

        Tweets.query(function(tweets) {
            console.log("Tweets at Angular Controller: " + tweets.length);

            $scope.tweets = tweets;
        });
    }
    else{
        console.log("Unauthorized");

        $location.path('/signin');
    }

};
}]);

2 个解决方案

#1


18  

Found the answer to my redirect issue, I swapped $location.path for

找到我的重定向问题的答案,我交换了$ location.path

$window.location.href = '/signin';

#2


8  

For my purposes calling $window.location.href = '/newpath'; from inside my controller was not working.

为了我的目的,调用$ window.location.href ='/ newpath';从我的控制器内部无法正常工作。

I've been using the following function if I need to reload within the controller:

如果我需要在控制器中重新加载,我一直在使用以下功能:

    $scope.changeRoute = function(url, forceReload) {
        $scope = $scope || angular.element(document).scope();
        if(forceReload || $scope.$$phase) { // that's right TWO dollar signs: $$phase
            window.location = url;
        } else {
            $location.path(url);
            $scope.$apply();
        }
    };

Then you would call it like this:

然后你会这样称呼它:

    $scope.changeRoute('#/newpath');

I will say though that doing this should be avoided, and that adding a run phase to your app's configuration phase should be preferred. You can read more about adding a run phase to your app configuration here: http://www.angularjshub.com/examples/modules/configurationrunphases/

我会说,应该避免这样做,并且应该首选在应用程序的配置阶段添加运行阶段。您可以在此处阅读有关向应用配置添加运行阶段的更多信息:http://www.angularjshub.com/examples/modules/configurationrunphases/

#1


18  

Found the answer to my redirect issue, I swapped $location.path for

找到我的重定向问题的答案,我交换了$ location.path

$window.location.href = '/signin';

#2


8  

For my purposes calling $window.location.href = '/newpath'; from inside my controller was not working.

为了我的目的,调用$ window.location.href ='/ newpath';从我的控制器内部无法正常工作。

I've been using the following function if I need to reload within the controller:

如果我需要在控制器中重新加载,我一直在使用以下功能:

    $scope.changeRoute = function(url, forceReload) {
        $scope = $scope || angular.element(document).scope();
        if(forceReload || $scope.$$phase) { // that's right TWO dollar signs: $$phase
            window.location = url;
        } else {
            $location.path(url);
            $scope.$apply();
        }
    };

Then you would call it like this:

然后你会这样称呼它:

    $scope.changeRoute('#/newpath');

I will say though that doing this should be avoided, and that adding a run phase to your app's configuration phase should be preferred. You can read more about adding a run phase to your app configuration here: http://www.angularjshub.com/examples/modules/configurationrunphases/

我会说,应该避免这样做,并且应该首选在应用程序的配置阶段添加运行阶段。您可以在此处阅读有关向应用配置添加运行阶段的更多信息:http://www.angularjshub.com/examples/modules/configurationrunphases/