Javascript 销毁在ngDialog模式中启动的AngularJS$间隔
我有一个显示数据表的模式。底层数据使用$http和$interval每隔几秒钟更新一次,但仅在显示模式时更新。我正在使用模态库。我需要在模式关闭时停止更新,但我不知道触发此破坏的最佳方式。一些代码:Javascript 销毁在ngDialog模式中启动的AngularJS$间隔,javascript,angularjs,ng-dialog,Javascript,Angularjs,Ng Dialog,我有一个显示数据表的模式。底层数据使用$http和$interval每隔几秒钟更新一次,但仅在显示模式时更新。我正在使用模态库。我需要在模式关闭时停止更新,但我不知道触发此破坏的最佳方式。一些代码: $scope.openModal = function() { var refreshModal = function() { $http({ params: {proj_id: $scope.projectId}, url: '/projects/_get_da
$scope.openModal = function() {
var refreshModal = function() {
$http({
params: {proj_id: $scope.projectId},
url: '/projects/_get_data',
method: 'GET'
})
.success(function(data, status, headers) {
$scope.modalData = data.data;
$scope.updateModal()
})
.error(function(data, status, headers) {
console.log('it didnt work');
});
}
refreshModal();
$interval( function() { refreshModal(); }, refreshRate);
ngDialog.open({ template: 'ModalId', scope: $scope, className: 'table-modal', showClose: false });
};
如何检测模式关闭
谢谢。最好将$interval的实例传递到模式关闭事件中,并在关闭模式时终止它 比如:
var interval = $interval( function() {refreshModal(); }, refreshRate);
var dialog = ngDialog.open(...);
$rootScope.$on('ngDialog.closed', function (e, $dialog) {
//identify if the dialog instance closing is the one the interval is for
if ( areTheSame(dialog, $dialog) ){
$interval.cancel(interval);
}
});
编辑:针对@zbycz
另一个更干净的解决方案是使用以下事实,即对话框具有:
完美的非常感谢,我不知道你能把一个亲密的事件和模态联系起来。这不是一个干净的解决方案。该事件会对任何关闭的对话框作出反应,因此,如果您在顶部再放置一个对话框,则关闭事件也会触发。@zbycz您是对的,应该有代码检查新关闭的对话框是否是我们关心的对话框。
var interval = $interval( function() {refreshModal(); }, refreshRate);
var dialog = ngDialog.open(...);
dialog.closePromise.then(function(){
$interval.cancel(interval);
});