angularjs 从未收到广播
angularjs broadcast never received
我有两个注入器,来自一个作用域的事件永远不会到达另一个作用域,即使它们应该是相同的作用域...
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);
}]);
更新
我的主要 objective 是知道 google 地图何时加载和初始化...
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 没有设置
仅注入 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);
}]);
两个作用域不一样
I have to broadcast on $rootScope from a global function
您可以通过 DOM 元素获取应用程序的注入器。例如:
angular.element(document.body).injector()
您可以调用 get
来获取 angular 服务并使用它,而不是 invoke
。绝对比 "raw" 全局函数更好。
我有两个注入器,来自一个作用域的事件永远不会到达另一个作用域,即使它们应该是相同的作用域...
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);
}]);
更新
我的主要 objective 是知道 google 地图何时加载和初始化...
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 没有设置
仅注入 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);
}]);
两个作用域不一样
I have to broadcast on $rootScope from a global function
您可以通过 DOM 元素获取应用程序的注入器。例如:
angular.element(document.body).injector()
您可以调用 get
来获取 angular 服务并使用它,而不是 invoke
。绝对比 "raw" 全局函数更好。