javascript - Changing Angular $location.path in response to a push service -
i making angular/ionic app needs change views in response push notification google's cloud messaging service. each view has it's on associated controller.
at moment, code set following: have push service, receives push notification, , broadcasts message on root scope interested controllers:
self.onnotificationgcm = function(data){ switch(data.event){ case "cancelled": $rootscope.$broadcast("ordercancellation", data.payload); break; case "completed": $rootscope.$broadcast("ordercompleted", data.payload); break; default: break; } }
on particular controller, listener might this:
$scope.$on("ordercompleted", function(event, data){ $location.path('completedorder'); $rootscope.$apply(); }
the problem code causes error when event fired:
cannot read property '$$nextsibling' of null
it seems if change of location within listener causing current scope destroyed before phase complete. there way make work properly?
move $apply wrap $broadcast. it's better $apply broadcaster, saves need $apply handling method in subscribers. in addition, it's better wrap applied code in $apply, have benefit of angular's error handling.
self.onnotificationgcm = function(data){ switch(data.event){ case "cancelled": $rootscope.$apply(function() { // wrap in $apply $rootscope.$broadcast("ordercancellation", data.payload); }); break; case "completed": $rootscope.$apply(function() { // wrap in $apply $rootscope.$broadcast("ordercompleted", data.payload); }); break; default: break; } }
Comments
Post a Comment