Seeking an explanation on the functionality of $scope.$on
and how $destroy
works in two separate controllers. When switching routes, a new controller is invoked, leading to the activation of $destroy
.
Could someone shed some light on how $interval
is initialized? It appears to be defined as a variable within promiseA but still initializes itself. These lines of code were directly copied for compliance purposes, any clarification would be greatly appreciated.
var myApp=angular.module('myApp',['ngRoute']);
myApp.controller('myController',function($scope){
});
myApp.controller('firstPage',function($scope,$interval){
$scope.thisMessage="First Message";
$scope.firstcall=function(){
var promiseA=$interval(function(){
console.log("First Page");
},10000);
$scope.$on('$destroy',function(){
$interval.cancel(promiseA);
});
}
});
myApp.controller('secondPage',function($scope,$interval){
$scope.thisMessage="Second Message";
$scope.thisMessage="Second Message";
$scope.secondcall=function(){
var promiseB=$interval(function(){
console.log("Second Page");
},10000);
$scope.$on('$destroy',function(){
$interval.cancel(promiseB);
});
}
});
myApp.config(function($routeProvider){
$routeProvider.when("/",{
templateUrl:"First.html"
})
.when("/second",{
templateUrl:"Second.html"
});
});