AngularJS位置在AppCtrl中重定向,无需加载当前控制器

时间:2014-06-23 06:41:52

标签: angularjs angularjs-routing

我对AngularJS很新, - 在我的AngularJS应用程序中,如果我们必须重定向,我会检查我的AppCtrl控制器。

.controller('AppCtrl', function($scope, $location) {
    if(window.localStorage.getItem("lastStation")) {
        $location.path('/app/player/'+window.localStorage.getItem("lastStation"));
    }
    ....

.controller('PlayerCtrl', function($scope, $stateParams) {
    alert($stateParams); // This one is alerted TWICE if the $location.path is called in the AppCtrl controller

希望有人可以提供帮助! ;)

2 个答案:

答案 0 :(得分:0)

您必须为应用程序配置路由,例如:

yourApp.config(['$routeProvider',
  function($routeProvider) {
    $routeProvider.
      when('/app/player/:lastLocation', {
        templateUrl: 'path_to_your_view',
        controller: 'PlayerCtrl'
      }).
      .....
  }]);

答案 1 :(得分:0)

现在我明白了;)

.run(function($rootScope, $location) {
    if(window.localStorage.getItem("lastStation")) {
        $location.path('/app/player/'+window.localStorage.getItem("lastStation"));
    }
});

我必须使用.run方法进行检查。