javascript - Destroying an AngularJS $interval which is started in an ngDialog modal -
i have modal showing data table. underlying data updated every few seconds using $http , $interval, when modal showing. using ngdialog library modals. need update stop on modal close, don't know best way trigger destruction. code:
$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 }); };
how can detect modal closing?
thanks.
the best thing pass instance of $interval modal close event, , upon closing modal, kill it.
something like:
var interval = $interval( function() {refreshmodal(); }, refreshrate); var dialog = ngdialog.open(...); $rootscope.$on('ngdialog.closed', function (e, $dialog) { //identify if dialog instance closing 1 interval if ( arethesame(dialog, $dialog) ){ $interval.cancel(interval); } });
edit: @zbycz another, cleaner solution use fact dialog has closepromise:
var interval = $interval( function() {refreshmodal(); }, refreshrate); var dialog = ngdialog.open(...); dialog.closepromise.then(function(){ $interval.cancel(interval); });
Comments
Post a Comment