angularjs - Destroy angular $http success/error snippets after route change -
the issue can seen here: http://embed.plnkr.co/qcw1ya/
i run route1 , have $timeout function. switch route2, delayed code route1 shows up. want destroy code running in $timeout function, in real case scenario $http service request , shows error messages previous routes.
solution here is: clean after ourselves.
best practices
...
add teardown code controllers , directives
controller , directives emit event right before destroyed. given opportunity tear down plugins , listeners , pretty perform garbage collection.
subscribe $scope.$on('$destroy', ...) event
so, instead of (there a updated plunker)
controller: function($timeout) { $timeout(function() { alert("hey i'm message route 1!"); }, 5000) }
we should this:
controller: function($scope, $timeout) { var removetimer = $timeout(function() { alert("hey i'm message route 1!"); }, 5000) $scope.$on('$destroy', function(){ $timeout.cancel(removetimer); console.log('all cleared') }); }
not saying - $http has cancel... later or sooner come server...
the point is, if there ghost actions triggered when repsonse comes (inside of .then()
) should either clear them or check if state has not gone...
check here
Comments
Post a Comment