即使关闭指令后服务也不会停止

service not stopping even after closing the directive

我有一个模态window,其中有一个指令,我在其中调用一个服务,该服务每 30 秒后在 $interval 内发出 http 请求,但是当我关闭模态时 window,每 30 秒后服务仍然是 运行,有没有办法在关闭模式 window 的同时停止它?

angular 包装的 interval 应该明确销毁,如文档中所述:

Intervals created by this service must be explicitly destroyed when you are finished with them. In particular they are not automatically destroyed when a controller's scope or a directive's element are destroyed.

删除指令时应该取消间隔,像这样:

var stop = $interval(myFunc, 1000)

$scope.on('$destroy', () => {
    $interval.cancel(stop);
});