【问题标题】:unable to show loading spinner image when the page is loading for the second time第二次加载页面时无法显示加载微调器图像
【发布时间】:2017-07-19 20:32:16
【问题描述】:

我想在页面加载/等待从后端获取响应时显示加载微调器。 下面的代码加载 pdf 文件并显示在网页上。最初,当 pdf 仍在加载时,会看到加载图标。但是第二次单击“重新生成”按钮以重新生成 pdf 并再次加载 pdf 时,旧的 PDF 出现在网页上,而不是我想显示加载微调器图标然后加载响应(而不是显示旧的 pdf 然后加载回复)。 或者我什至可以在 Reload data button 附近显示加载微调器图标,直到加载新的响应。

html代码:

    <table style="width:100%;height:80%;" ng-controller="loadingSampleCtrl">
<tr> <td><button type="button" ng-click="fileRead()">Reload data</button></td></tr>

         <tr ng-show="loading"> <td colspan="5" style="white-space: nowrap;text-align: center; font-size:36px;"><div class="loader"><i class="fa fa-spinner fa-spin" ></i> Loading<span class="loader__dot">.</span><span class="loader__dot">.</span><span class="loader__dot">.</span></div></td></tr>
         <object-reloadable></object-reloadable>
         </table>

js代码:

app.directive('objectReloadable', function() {
      var link = function(scope, element, attrs) {
        var currentElement = element;

        scope.$watch('pdfName', function(newValue, oldValue) {
             if (newValue !== oldValue) {
                 scope.loading = false;
                 scope.pdfName = newValue;
                 var html = '<object style="width: 100%; height: 1200px;overflow-y: hidden; cursor:pointer;" type="application/pdf" data="' + scope.pdfName.filePath + '" ></object>';
                 var replacementElement = angular.element(html);
                currentElement.replaceWith(replacementElement);
                currentElement = replacementElement;
            }
        });
    };
    return {
        restrict: 'E',
        scope: false,
        link: link
    };
})

myApp.controller('loadingSampleCtrl', function ($scope, FilesService) {
      $scope.loadData = function () {
      $scope.loading = true;
        FilesService.fileRead().then(
            function (response) {
                 $scope.filePathAndName = response;
                   if(window.navigator.msSaveOrOpenBlob){
                    $scope.IEBrowser = true;
                    $timeout(function() {
                        $scope.pdfName = response;
                        var html = '<object type="application/pdf" data="' + newValue + '"></object>';
                         var replacementElement = angular.element(html);
                         currentElement.replaceWith(replacementElement);
                         currentElement = replacementElement;*/
                    }, 0);
                } else {
                    $scope.IEBrowser = false;
                    $scope.filePathAndName = response;
                     $scope.loading = false;
                 }
            },
          function (errResponse) {
                $rootScope.showError("Internal error" + errResponse);
            });

    }
   $scope.loadData();
});

加载微调器的css:

<style>
    @keyframes blink {50% { color: transparent }}
    .loader__dot { animation: 1s blink infinite }
    .loader__dot:nth-child(2) { animation-delay: 250ms }
    .loader__dot:nth-child(3) { animation-delay: 500ms }

</style>

我尝试过使用$scope.loading = true;(或相应地为假),但结果不如预期。

--编辑-- 下面是更新的js代码: Reload按钮的OnClick,调用loadingSampleCtrl,$scope.loadData = function ()

更新js代码:

app.directive('objectReloadable', function() {
          var link = function(scope, element, attrs) {
            var currentElement = element;

            scope.$watch('pdfName', function(newValue, oldValue) {
                 if (newValue !== oldValue) {
                     scope.loading = false;
                     scope.pdfName = newValue;
                     scope.html = '<object style="width: 100%; height: 1200px;overflow-y: hidden; cursor:pointer;" type="application/pdf" data="' + scope.pdfName.filePath + '" ></object>';
                     var replacementElement = angular.element(scope.html);
                    currentElement.replaceWith(replacementElement);
                    currentElement = replacementElement;
                }
            });
        };
        return {
            restrict: 'E',
            scope: false,
            link: link
        };
    })

    myApp.controller('loadingSampleCtrl', function ($scope, FilesService) {
          $scope.loadData = function () {
          $scope.loading = true;
$scope.html='';
            FilesService.fileRead().then(
                function (response) {
                     $scope.filePathAndName = response;
                       if(window.navigator.msSaveOrOpenBlob){
                        $scope.IEBrowser = true;
                        $timeout(function() {
                            $scope.pdfName = response;
                           }, 0);
                    } else {
                        $scope.IEBrowser = false;
                        $scope.filePathAndName = response;
                         $scope.loading = false;
                     }
                },
              function (errResponse) {
                    $rootScope.showError("Internal error" + errResponse);
                });

        }
       $scope.loadData();
    });

使用上面提到的 js 代码,我也面临同样的问题。点击重新加载按钮,看不到加载图标。

【问题讨论】:

  • 您是否尝试过在单击按钮时取消设置 pdfname 变量(将其设置为空对象)?
  • 不,我还没有厌倦它。我该怎么做,因为我是在运行时动态创建的。任何建议都会有所帮助。
  • 如果您可以为您的代码创建一个小提琴,那么帮助您会容易得多。

标签: javascript html angularjs twitter-bootstrap


【解决方案1】:

编辑:我在下面提到过在 ng-click 上调用一个您似乎错过的函数。

这应该可行:

Reload data button被点击时,调用如下函数:

用 $scope.html 替换 var html; 您正在声明 html 变量两次;

function reloadData(){
  $scope.pdfName = {};
  $scope.html = '';
  $scope.loadData();
}

请根据您的代码更改变量的名称。

【讨论】:

  • 请看我的html代码,我刚刚添加了按钮代码, 我正在调用我在生成 pdf 时第一次实际使用的同一个 fileRead() 按钮。我还可以使用您建议的解决方案吗?
  • 在您的控制器中添加上述功能并将其映射到您的按钮,它应该可以工作。
  • 我刚刚注意到我的代码或您的代码,当我单击“重新加载数据”按钮时,它显示加载图标,但它显示在下方,我需要向下滚动页面才能看到它。我认为它在 pdf 显示区域之后显示加载图标..有什么建议吗?
  • 同时清除 html 变量。包括 var html = ' ';在你的函数 reloadData.检查答案,我已对其进行编辑以添加此案例。
  • 仍然是同样的问题,请看我上面的帖子,我在帖子的编辑部分给出了更新的 js 代码。仍然无法显示重新加载图标代替 html 部分,而是显示的 pdf 部分变为空白,并且重新加载图标显示到该空白区域,我需要向下滚动才能看到该图标。
【解决方案2】:

在 ng-click 上,您直接调用 fileread。而是调用 loadData。那么它应该可以工作(原因是您在 loadData 中将 $scope.loading 设置为 true)

【讨论】:

    猜你喜欢
    • 2013-04-10
    • 2019-11-16
    • 1970-01-01
    • 2014-03-17
    • 2022-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多