Angular Js中的$window.location成功消息

$window.location success message in Angular Js

本文关键字:location 成功 消息 window Js 中的 Angular      更新时间:2023-09-26

我使用$window.location(url)直接从rest api下载csv文件。

我需要显示成功消息,即使我使用promise,文件正在下载,但我无法显示成功消息。

function download() {
  getcsv().then(function(response) {
    if (response === 'success') {
      $scope.msg = 'success';
    }
  })
}
getCSV() {
  var defer = $q.defer;
  downloadCSV();
  return defer.promise;
}
function dowloadcsv() {
  $window.location(url);
}

您可以使用$timeout服务来返回promise:

app.factory('downloadService', function($q, $window, $timeout) {
    return {
    download: function(url) {
        var defer = $q.defer();
        $timeout(function () {
            $window.location.href = url;
        }, 1000)
            .then(function () {
                defer.resolve('success');
            }, function () {
                defer.reject('error');
            });
        return defer.promise;
        }
    }
}

如果上面的代码不是一个替代方案,请尝试在您的downloadcsv函数中使用$q.resolve。