5

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

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

我的角控制器

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');
    }

};
}]);
4

2 回答 2

18

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

$window.location.href = '/signin';
于 2014-01-03T09:45:11.517 回答
8

出于我的目的,从我的控制器内部调用$window.location.href = '/newpath';不起作用。

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

    $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();
        }
    };

然后你会这样称呼它:

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

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

于 2014-05-22T18:47:48.663 回答