Safe to pass current $scope from controller to angularjs service function -
within angular controller attaching websocket service. when controllers scope destroyed want remove subscription.
is safe pass current scope service subscription function can auto remove on scope destroy? if dont each controller attaches socket listener has remember clean up.
basically safe pass current $scope service function or there better way of doing this?
i had similar need in project. below object returned in angularjs factory (which initializes websocket). onmessage
method automatically unsubscribes callback
if pass in associated scope
in second argument.
io = onmessage: (callback, scope) -> listeners.push callback if scope scope.$on "$destroy", => @offmessage callback offmessage: (callback) -> listeners.remove callback
the javascript equivalence below.
var io = { onmessage: function(callback, scope) { var _this = this; listeners.push(callback); if (scope) { scope.$on("$destroy", function() { _this.offmessage(callback); }); } }, offmessage: function(callback) { listeners.remove(callback); } };
Comments
Post a Comment