javascript - $scope.$on & controller load timing/speeds -
i'm curious timing of when angular controllers loaded versus $scope/event listeners in controllers. could provide documentation or explanation how works?
i've looked while, , not find clear answer on this.
i have example load filter options database, , $emit parent controller, $broadcast event other controllers when occurs. (code abbreviated)
filtercontroller.js
app.controller("filtercontroller", ["$scope", "$q", function($scope, $q) { (function getinitialfilters() { $q.all([ // few promises call database information , sets filters ]).then(function(){ // after promises finish, $emit event parent controller, // appcontroller $scope.$emit("heyappcontrollerfiltershaveloaded") }) }(); }])
appcontroller.js (parent controller of filtercontroller , dashboardcontroller)
app.controller("appcontroller", ["$scope", function($scope) { $scope.$on("heyappcontrollerfiltershaveloaded", function() { $scope.$broadcast("allchildcontrollersfiltershaveloaded"); console.log('broadcast sent'); }); }]);
the problem is, when $broadcast occurs, $scope.$on / event listener in child controller misses it, though seems controller has loaded , $emit , $broadcast occurs after longest roundtrip database finishes. (granted, it's not long of trip)
dashboardcontroller.js
app.controller("dashboardcontroller", ["$scope", function($scope) { console.log('dashboardcontroller loaded'); $scope.$on("allchildcontrollersfiltershaveloaded", function() { console.log('listener listened'); dosomestuffwithfilters(); }); }]);
the console prints out in following order:
"dashboardcontroller loaded"
"broadcast sent"
the quick fix solution found here add 500ms timeout $broadcast, , dashboardcontroller catches event.
my question though, controller load, , takes while add $scope.$on / event listener? longer short roundtrip database , longer registering $emit , $broadcast in other controllers? or how timing work here?
Comments
Post a Comment