Angular UI-Router:无法从服务访问状态名称

时间:2015-06-10 17:06:47

标签: angularjs angular-ui-router

我想从服务中检测当前状态的名称。

问题:$state.current.name返回null。

app.factory('rootUserService', ['$http', '$state', '$stateParams', '$rootScope', '$q', 'socketFactory', function($http, $state, $stateParams, $rootScope, $q, socketFactory) {
 return {

  guestOrNot: function(res) {
    if ( res._id === 'someid' ) { 
      $rootScope.isGuest = true;
    } 
    else {
      $rootScope.isGuest = false;

      // if current state = app.landing, do something. 

      console.log($state) // current.name field is 'app.landing'
      console.log($state.current) // name field is empty
      console.log($state.includes('app.landing')) // returns false no matter what.
    };
  }

}]);

console.log($ state)返回以下内容: enter image description here

console.log($ state.current)返回以下内容 enter image description here

0 个答案:

没有答案
相关问题