简体   繁体   English

设置$ rootScope但值从另一个控制器不可见

[英]setting $rootScope but value then not visible from another controller

My application requires candidates to be set up with an agency and nationality (among other things). 我的申请要求候选人必须具有代理机构和国籍(除其他外)。 I want to set the list of agencies and nationalities in global variables so that they can be accessed by the profile screen rather than retrieved from the database every time (as they are currently). 我想在全局变量中设置机构和国籍的列表,以便可以通过概要文件屏幕访问它们,而不是每次(如当前那样)从数据库中检索它们。 Thanks to the other questions here I've got so far... but a crucial puzzle piece is clearly missing. 多亏了这里的其他问题,我到现在为止……但是显然缺少一个至关重要的难题。 The data is being retrieved from the DB but then isn't visible elsewhere. 正在从数据库中检索数据,但在其他地方看不到该数据。

homecontroller.js which loads up the data... homecontroller.js加载数据...

//Home page Controller
function HomeCtrl($scope, $rootScope, SessionTimeoutService, GetAllAgencies, GetNationalityList){

    GetAllAgencies.getData({}, function(agencieslist, $rootScope) {
        SessionTimeoutService.checkIfValidLogin(agencieslist);
        $rootScope.agencieslistglobal = agencieslist.data;
    });

/*  I tried hard-coding values here - that worked & was passed thro' ok  
    $rootScope.nationalitieslistglobal = [
        {'nationality_id' : 0, 'name' : 'Unknown'},
        {'nationality_id' : 1, 'name' : 'Known'},
        {'nationality_id' : 2, 'name' : 'Pants'},
    ]; */
    GetNationalityList.getData({}, function(nationalitieslist, $rootScope) {
        SessionTimeoutService.checkIfValidLogin(nationalitieslist);
        $rootScope.nationalitieslistglobal = nationalitieslist.data;
    });

    alert($rootScope.nationalitieslistglobal[8].name);
}

The alert doesn't fire at all. 警报根本不会触发。

From the candidatescontroller.js: 从候选人控件:

function CandidatesAddCtrl($scope, CandidateModel, GetNationalityList,      SessionTimeoutService, $http, GetAllAgencies, $rootScope) {
/* commented out as this should now be done globally - this works when it's in place
    GetAllAgencies.getData({}, function(agencieslist) {
        SessionTimeoutService.checkIfValidLogin(agencieslist);
        $scope.agencieslist = agencieslist.data;
    });
*/
    CandidateModel.getBlankCandidate();
    $scope.candidateinfo = CandidateModel;

    // get global agencies list
    $scope.agencieslist = $rootScope.agencieslistglobal;
    alert($scope.agencieslist); // shows 'undefined'

    /*
        GetNationalityList.getData({}, function(nationalitieslist) {
            SessionTimeoutService.checkIfValidLogin(nationalitieslist);
            $scope.nationalitieslist = nationalitieslist.data;
        });
    */
    // get global nationalities list
    $scope.nationalitieslist = $rootScope.nationalitieslistglobal;
....
}

So... when I hard-code the data outside of the GetNationalityList.getData function (the bit that's commented out in the example above), it is populated & passed through OK & my drop-down list populates. 因此,...当我在GetNationalityList.getData函数(在上面的示例中已注释掉的位)之外对数据进行硬编码时,将填充并通过OK传递数据,然后填充我的下拉列表。 When I don't do that, the $rootScope values are 'undefined'. 当我不这样做时,$ rootScope值是'undefined'。

I have 2 theories - 我有2个理论-

  1. somehow the homecontroller $rootScope isn't being recognised as the global I'm intending (that's just the name of the variable) and some other "passing back" action needs to be taken (I've tried several variations on "return nationalitieslist.data" and assigning it to other variables/handles). 某种程度上,家庭控制器$ rootScope并没有被认为是我想要的全局变量(这只是变量的名称),还需要采取其他一些“回传”操作(我已经尝试过“返回国籍列表”的几种变体)。数据”,并将其分配给其他变量/句柄)。 Also, nationalitieslist.data is in the same format as the hard-coded list, only it's longer. 另外,nationalitieslist.data与硬编码列表的格式相同,只是更长。

  2. I'm being caught out by the asynchronous nature of javascript and when I load the second page, the data just isn't there yet. 我被javascript的异步特性所吸引,当我加载第二页时,数据还不存在。 I'm not convinced this is right as the DB call is finishing and I had an alert which showed me a random nationality name and it was there. 我不确信这是正确的,因为数据库呼叫正在结束,并且我有一个警报向我显示了一个随机的国籍名称,并且该名称在那里。

My frustration in part is coming from the fact that the GetNationalityList and GetAllAgencies code snippets work and assigns values correctly in the candidate controller (the bits that are now commented out), but are working subtly differently in the homecontroller. 我之所以感到沮丧,部分原因在于GetNationalityList和GetAllAgencies代码段在候选控制器(现在已注释掉的位)中正常工作并正确分配了值,但在家庭控制器中却以不同的方式工作。

Top tips, anyone, please? 重要提示,有人吗?

I am sure this article can help. 我相信这篇文章会有所帮助。 http://toddmotto.com/all-about-angulars-emit-broadcast-on-publish-subscribing/ I had somewhat of a similar issue but I needed to emit from a factory. http://toddmotto.com/all-about-angulars-emit-broadcast-on-publish-subscribing/我有一个类似的问题,但我需要从工厂排放。 My problem was answered here. 我的问题在这里得到了回答。

Angular $rootScope.$on Undefined 角$ rootScope。$ on未定义

Your 2nd point is more your issue, although it's not JavaScript that's async in nature, it's Angular services such as your GetAllAgencies service. 第二点更多的是您的问题,尽管本质上不是异步的JavaScript,而是Angular服务,例如GetAllAgencies服务。 You're making async calls but not waiting for the response. 您正在进行异步调用,但不等待响应。 That's why when you assign it manually it all works. 这就是为什么当您手动分配它时都能正常工作的原因。 Try putting your alert() inside the callback function to the getData() call. 尝试将您的alert() 放在getData()调用的回调函数中。 This should highlight how the process should work. 这应该突出该过程应如何工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将rootScope值从一个控制器传递到另一个控制器 - Passing rootScope value from one controller to another controller 使用$ rootscope将变量从一个控制器传递到另一个控制器 - Passing variables from one controller to another with $rootscope 无法从其他控制器访问$ rootScope - Can't access $rootScope from another controller 从没有rootScope的另一个控制器更改已经加载的控制器中的作用域值 - change scope value in already loaded controller from another controller without rootScope 使用angularjs中的$ rootScope将值从一个不同的应用程序模块控制器传递到另一个应用程序模块服务 - pass value from one different app module controller to another app module service using $rootScope in angularjs $ rootscope值在Controller中未定义 - $rootscope value undefined in Controller 通过rootScope将变量从控制器传递到另一个控制器 - Pass variable from controller to another controller via rootScope 将$ rootScope值存储在localstorage中,并在页面刷新时在另一个控制器中使用它 - To store $rootScope value inside localstorage and using it in another controller on page refresh 如何在Angular单元测试中从另一个控制器的rootScope向rootScope添加方法? - How to add a method into rootScope from the rootScope from another controller in an Angular unit test? 通过$ rootScope将数据从一个控制器传递到另一个控制器 - Passing data from one controller through another through $rootScope
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM