如何在 Angular Service 函数中设置 $rootScope? [英] How do you set $rootScope within Angular Service function?

查看:23
本文介绍了如何在 Angular Service 函数中设置 $rootScope?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为 Angularjs 设置 $rootScope 时遇到问题.

下面是我的函数

App.controller('Controller',函数 (UtilityService, $rootScope) {var setSession = 函数 () {$rootScope.test = "是";//<-- 我明白了UtilityService.getSession().success(功能 () {$rootScope.test = "否";//<-- 我不明白如何获取/设置这个值?});};设置会话();});

附加信息:

一种可行的方法是设置一个在多个控制器之间交互的服务.有没有人知道如何使用返回 http.get json 对象的服务来执行此操作.

我无法在服务中实例化的控制器中获取动态范围.

解决方案

为了解决我的问题,我不得不

1) 将 $rootScope 传入我的第二个控制器

<块引用>

App.controller($rootScope) {

2) 将我的第二个控制器的功能设置为 $rootScope

<块引用>

$rootScope.functionCall = function(){};

3) 将我传递的值设置为 $rootScope ($rootScope.orderId)

<块引用>

$rootScope.functionCall = function() {Service.getItems($rootScope.orderId).success(功能(结果){$scope.items = 结果;});};

<小时>

4) 在我的实用程序控制器中,我遍历结果,解析它们,并将它们设置为 $rootScope,正如您在 #3 中看到的那样,我正在初始化$rootScope.orderId"

<块引用>

angular.forEach(results, function (value, key) {如果(键!= null){$parse(key).assign($rootScope, value);}});

5) 我正在从我的服务调用中重新调用控制器的函数! 这就是我将变量置于范围内"的神奇之处

<块引用>

$rootScope.functionCall();

6) 我也在测试该函数是否存在导致不同的页面使用实用程序代码但可能没有执行该函数

<块引用>

if (typeof $rootScope.functionCall == 'function')

var setSession = function () {UtilityService.getSession().success(功能(结果){//将 rootscope 会话放在作用域中angular.forEach(结果,函数(值,键){如果(键!= null){$parse(key).assign($rootScope, value);}});//必须绕过范围"问题,因此必须执行这些 fns()if (typeof $rootScope.functionCall == 'function') {$rootScope.functionCall();}});};设置会话();

I'm having trouble setting $rootScope for Angularjs.

Below is my function

App.controller('Controller',
function (UtilityService, $rootScope) {

var setSession = function () {

  $rootScope.test = "yes"; // <-- I get this

  UtilityService.getSession().success(
  function () {       
    $rootScope.test = "No"; // <-- I don't get this How do I get/set this value?       
  });      
};

setSession();   

});

Additional Info:

One of the ways that might work is to set up a service that is interacted between multiple controllers. Does anybody know how to do this with the service returning an http.get json object.

I'm having trouble getting a dynamic scope in my controller that is instantiated within a service.

解决方案

In order to address my issue I had to

1) Pass $rootScope into my 2nd controller

App.controller($rootScope) {

2) Set my 2nd controller's function to $rootScope

$rootScope.functionCall = function () {};

3) Set my passed value to $rootScope ($rootScope.orderId)

$rootScope.functionCall = function () {
 Service.getItems($rootScope.orderId).success(
    function(results) {
      $scope.items = results;
    });
};


4) within my utility controller, I loop through my results, parsing them, and setting them to $rootScope as you can see in #3 I am initializing "$rootScope.orderId"

angular.forEach(results, function (value, key) {
      if (key != null) {
        $parse(key).assign($rootScope, value);
      }
    });   

5) I am re-calling the controller's function from within my service call! This is what did the magic for me putting my variable "in scope"

$rootScope.functionCall();

6) I am also testing to see if the function exist cause different pages utilize the utility code but may not have the function to execute

if (typeof $rootScope.functionCall == 'function')

var setSession = function () {

  UtilityService.getSession().success(
  function (results) {             

    // Place the rootscope sessions in scope
    angular.forEach(results, function (value, key) {
      if (key != null) {
        $parse(key).assign($rootScope, value);
      }
    });                

    // Have to bypass "scope" issues and thus have to execute these fns()
    if (typeof $rootScope.functionCall == 'function') {
      $rootScope.functionCall();
    }

});

};
setSession();

这篇关于如何在 Angular Service 函数中设置 $rootScope?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆