我正在使用MEAN.io的样板堆栈,并发现它是一个很好的起点,但是我在混合不同的路由命令时遇到了麻烦.我的应用程序将有一个简单的登录页面是公开的,其他一切都隐藏在其后面.我可以检查用户是否经过身份验证没有问题,但我不能为我的生活让Angular从服务器加载登录页面.我已经在我的html页面上有一个signin按钮,调用正确的路径完全没有问题,我只是不能从代码中做同样的事情.
$ 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'); } }; }]);
Mark.. 18
找到我的重定向问题的答案,我换了$ location.path
$window.location.href = '/signin';
即使您不使用MEAN,也可以使用相同的机制.更好的标题是"如何从AngularJS中更改窗口位置?也许,也许不是,希望在页面上有这个,谷歌将排序. (5认同)
DrewT.. 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/
找到我的重定向问题的答案,我换了$ location.path
$window.location.href = '/signin';
为了我的目的,$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/