【发布时间】:2015-01-27 05:47:38
【问题描述】:
我正在通过指令将画廊放入页面。画廊使用 javascript 在图像之间来回解析(我计划使用 css 转换来旋转通过一组类)。
我想要一个 setInterval 函数执行,但只有当指令在页面上时(也就是说只有在调用某个路由时)。但是,即使点击了新路由,并且在我的页面上呈现了新视图,现有功能仍在执行。例如:
指令:
angular.module('app')
.directive('gallery', function ($location) {
return function (scope, element, attrs) {
setInterval(function () {
//This is where I would change the classes on the element
//This will continually execute, even when on a different view
}, 500);
}
});
HTML 视图
<div gallery>
Something special
</div>
有没有什么办法让我只在指令在页面上时执行间隔函数?
【问题讨论】:
-
请注意,您可能应该使用
$interval,但这并不能解决这个特定问题。 -
谢谢。这个提示把我带到了这里stackoverflow.com/questions/26447923/…,看起来很有希望....(明白了吗?)
标签: javascript angularjs