angularjs - Updated way to destroy a controller using the as and this syntax -
when using 'as' syntax , binding values on controller there new way destroy controller or should still using scope?
if declare controller in dom using 'as' syntax:
<section ng-controller="mycontroller mine">... and use 'this' syntax bind things controller instead of scope, how make sure controller destroyed?
myapp.controller('myapp', ['pubsub', function (pubsub) { var mycontroller = this; this.subject = "" this.mytopic = pubsub.subscribe('mytopic', function(data) { mycontroller.subject = data; } } this create memory leak, controller not cleaned because pubsub holding reference controller.
is there way create destroy function controller, or still scope object listen destroy event?
myapp.controller('myapp', ['pubsub', '$scope', function (pubsub, $scope) { ... $scope.$on("$destroy", function() { if (mycontroller.mytopic) { mycontroller.mytopic.unsubcribe(); mycontroller.mytopic = null; } });
using $scope special, such listen events, broadcasting, watching, emitting, etc, fine, when using controlleras syntax.
Comments
Post a Comment