任何人都可以請解釋一下$scope.$on
的工作情況以及$destroy
如何在這兩個控制器中工作。就像當我改變路線時,一個不同的控制器被調用,這就是爲什麼$destroy
被調用? $interval
如何得到初始化,我剛剛將它定義爲promiseA下的變量,但它被初始化。我只是複製這些行來滿足要求,任何人都可以幫助我理解這些代碼行嗎?
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"
});
});
嗨,彼得。非常感謝 – evilBops