【问题标题】:angularjs broadcast never received从未收到 angularjs 广播
【发布时间】:2015-07-09 04:40:39
【问题描述】:

我有两个注入器,来自一个范围的事件永远不会到达另一个范围,即使它们应该是相同的范围......

http://jsfiddle.net/rkLzww3t/

angular.injector(['ng']).invoke(['$rootScope', function($root) {
    alert('will try to be usefull');
    $root.$on('bevent123', function() {
      alert('useful bevent123 never happens');
    });
}]);

angular.injector(['ng']).invoke(['$rootScope', function($root) {
    $root.$on('bevent123', function() {
      alert('bevent123 done from local injector, but we were not usefull');
    });
    console.log("about to $root.$broadcast('bevent123')");
    window.setTimeout(function() {
      $root.$broadcast('bevent123');
    },1000);
}]);

更新

我的主要目标是了解谷歌地图何时加载和初始化...

var app = angular.module('module_name');

function googleCallback() {
  //run does not do anything if all modules are already loaded and initialized
  //angular.module('module_name').run(['$rootScope', function($root) {
  angular.injector(['ng']).invoke(['$rootScope', function($root) {
    console.log("about to $root.$broadcast('googleLoaded')");
    $root.googleLoaded = true;
    window.setTimeout(function() {
      $root.$broadcast('googleLoaded');
      //$root.$emit('googleLoaded');
    },1000);
  }]);
}

app.run(['$rootScope',function($root) {
  var element = document.createElement("script");
  element.type = 'text/javascript';
  element.src = "https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=places&callback=googleCallback";
  element.async = true;
  var domElement = document.head || document.getElementsByTagName("head")[0];
  domElement.appendChild(element);
}]);

app.controller('Controller', ['$scope','$rootScope', function ($scope, $root) {
  function initMaps() {
    console.log("initMaps()");
  }
  $scope.$on('$viewContentLoaded', function() {
    console.log('$viewContentLoaded');
    if (!$root.googleLoaded) {
      console.log("waiting for googleLoaded");
      $scope.$on("googleLoaded", function() {
        console.log("about to init");
        initMaps();
      });
    } else {
      initMaps();
    }
  });
}]);//end of app.controller('Controller'...

一切正常,直到 "about to init" 永远不会被记录到控制台并且 api 不会得到设置

【问题讨论】:

  • 在超时时尝试$root.$emit('bevent123');
  • $emit 在 jsfiddle 上具有相同的行为
  • 从注入器到控制器之间没有关系,所以rootScope.emit或广播在scope.on上不起作用,尝试使用$rootScope.$on()
  • @ritesh 实际的问题是可以有多个“$rootScope

标签: angularjs angularjs-scope angular-broadcast


【解决方案1】:

这两个作用域不一样。

我必须通过全局函数在 $rootScope 上广播

您可以通过 DOM 元素获取应用程序的注入器。例如:

angular.element(document.body).injector()

您可以调用get 而不是invoke 来获取角度服务并使用它。绝对比“原始”全局函数更好。

【讨论】:

  • 我尝试从 document.body 注入器进行广播,但没有收到,然后我尝试从模块模板中的一个元素进行广播...仍然没有,所以最后我既广播又收听document.body 的注入器,现在它可以工作了...谢谢。
【解决方案2】:

只注入一次 ng 并在同一个实例上调用。它现在可以工作了。

var app = angular.injector(['ng'])

app.invoke(['$rootScope', function($root) {
    alert('will try to be usefull');
    $root.$on('bevent123', function() {
      alert('useful bevent123 never happens');
    });   }])

app.invoke(['$rootScope', function($root) {
    $root.$on('bevent123', function() {
      alert('bevent123 done from local injector, but we were not usefull');
    });
    console.log("about to $root.$broadcast('bevent123')");
    window.setTimeout(function() {
      $root.$broadcast('bevent123');
    },1000);

}]);

【讨论】:

  • 我必须通过全局函数在 $rootScope 上广播,我可以保存对目标模块的引用,但模块没有 invoke,模块 run 函数似乎很有希望,但它只是在模块加载/初始化期间工作,而不是在......
  • 是的,它只会在你的应用程序第一次初始化时调用一次,因为module.run只是在module.config之后被调用,当所有的服务和依赖都被注入和处理好了.为什么不在控制器中使用它?
  • 即使它是在 rootScope 上广播的,它也只是为它的单个对象广播到 rootScope。 angular.injector(['ng']) 每次调用都会创建一个新对象。 var A=angular.injector(['ng']), var B=angular.injector(['ng']), A !== B. 调用 invoke 方法只会将该函数添加到其中一个而不是另一个。您应该创建一个模块并传递该模块引用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多