Angular ui-router抽象状态$ scope未擦除

时间:2015-12-29 22:41:12

标签: angularjs ionic-framework

我正在用离子构建混合应用程序。 我有配置ui-router的侧面菜单布局如下 -

$stateProvider
    .state('login', {
      url: "/login",
      cache: false,
      controller: 'LoginCtrl',
      templateUrl: "templates/login.html",
      resolve: {
        requireNoAuth: function($state, Auth){
          return Auth.$requireAuth().then(function(auth){
            $state.go('menu.search');
          }, function(error){
            return;
          });
        }
      }
    })
    .state('menu', {
      url: '/menu',
      abstract:true,
      controller: 'AppCtrl',
      templateUrl: 'templates/menu.html',
      resolve: {
        auth: function($state, Auth){
          return Auth.$requireAuth().catch(function(){
            $state.go('login'); //if not authenticated send back to login
          });
        },
        profile: function(Users, Auth){
          return Auth.$requireAuth().then(function(auth){
            return Users.getProfile(auth.uid).$loaded(); //get profile for user.
          }).catch(function(){
            console.log("Auth failed");
          });
        }
      }
    })
    .state('menu.search', {
      url: '/search',
      views: {
        'menu': {
          templateUrl: 'templates/search.html',
          controller: 'SearchCtrl'
        }
      }
    });

这是我的AppCtrl

app.controller('AppCtrl', function($scope, $rootScope, auth, profile, AuthService, $state){
  $scope.user = profile;
  $scope.logout = function(){
    AuthService.logoutUser().then(function(){
      $state.go('login');
    });
  }
});

LogoutService -

logoutUser: function () {
            var d = $q.defer();
            self.current = {};
            Auth.$unauth();
            d.resolve();
            return d.promise;
        }

退出按钮 -

<ion-item ng-click = "logout()" menu-close="">Logout</ion-item>

侧边菜单需要“个人资料”对象,因此我可以在侧边菜单中显示信息。问题是,只有在用户第一次登录时才会实例化抽象状态控制器。这是一个场景

User 1 logs in - 
  User 1 profile loaded in side menu and properly displayed
  User 1 logout
Now without closing the window/ app
User 2 logs in -
   User 2 profile gets loaded in all child states but the side menu $scope.user still contains user 1 information. 

这是否意味着父控制器依赖项只解析一次?如何确保AppCtrl每次都能正确解析配置文件。

1 个答案:

答案 0 :(得分:0)

我自己找到了解决方案 - 我怀疑侧边菜单是“记住&#39;范围变量。所以我在cache: false状态配置中设置.menu,它不再记住以前的用户信息。