【问题标题】:$rootScope variable not saved after state transition$rootScope 变量在状态转换后未保存
【发布时间】:2015-03-21 15:31:21
【问题描述】:
.controller('FrameCtrl', ['$scope','$state','$rootScope', '$stateParams',
    function($scope, $state, $rootScope, $stateParams) {

        console.log($rootScope); // all the data is there.
        console.log($rootScope.activeAddress); // undefined
        console.log($rootScope.activeRestaurant); // undefined   

}])

我试图理解为什么,即使 $rootScope 对象同时包含 activeAddressactiveRestaurant 对象,当我专门 console.log() 它们时它们是空的。

在之前状态的控制器中,我使用这两行代码设置了这两个对象,如果有区别的话:

// Parse.com promise returns array of addresses after login.
$rootScope.activeAddress = data.addresses[0];
// After getting restaurant ID from activeAddress, copy object to activeRestaurant
$rootScope.activeRestaurant = $rootScope.aRestaurant;

简而言之,我想知道为什么 $rootScope.activeAddress$rootScope.activeRestaurant 返回 undefined,即使它们出现在 $rootScope 父对象中。

编辑: 以下是之前状态的 HTML 页面的标记:

<button class="button button-block button-positive" ng-click="login()" 
    ui-sref="frame.menu({menu: 'appetizers'})">
    LOG IN
</button>

【问题讨论】:

  • 我注意到,当您使用console.log 输出某些内容时,对象似乎具有在调用console.log 之后设置的属性。尝试克隆@rootScope 并输出克隆,您会看到它没有这些属性。例如:stackoverflow.com/questions/22146684/…
  • 你如何改变状态..??
  • @squiroid 我添加了 HTML
  • 尝试使用$timeout(function(){console.log($rootScope.activeAddress); console.log($rootScope.activeRestaurant);})打印它们

标签: angularjs controller parse-platform ionic-framework


【解决方案1】:

这是因为 Angular 加载 A-sync 并且控制器在 rootScope 之前加载。尝试使用 $q 查看 defer() 和 promises

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-06
    • 2021-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-30
    • 1970-01-01
    相关资源
    最近更新 更多