我有两个控制器。一个名为 page1 的页面和另一个名为 page2 的页面。在第 1 页上,我有一个运行动画的代码。我有一个无限调用自身的函数。在这一行。
.each('end', tick)当动画结束时,它再次启动 tick()。
function tick(){
console.log("hello");
.
.
.
//line 190
paths.attr('transform', null)
.transition()
.duration(duration)
.ease('linear')
.attr('transform', 'translate(' + x(now - (limit - 1) * duration) + ')')
.each('end', tick) //when the transition ends, it start tick() again
}
这很好,因为它是一个无限动画。我把console.log("hello")放在tick函数中,那么每次执行这个函数时,都会向控制台写入"hello"这个词。到目前为止,一切都很好 !。当我改变控制器时,即当我点击转到第2页时,我之前的动画仍在运行,我仍然可以看到我的第一个控制器中的函数的console.log("hello")。
更换控制器时如何停止此功能?
这是我的代码:
监听被破坏的范围并停止动画循环:
function MyController($scope) {
var animating = true;
$scope.$on('$destroy', function () {
animating = false;
});
function tick() {
if (!animating) return;
console.log("hello");
}
}
$destroy
在此处阅读 Angular 的作用域事件:https : //docs.angularjs.org/api/ng/type/$rootScope.Scope#$destroy
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句