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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -