【问题标题】:how to automatically refresh iframe angularjs?如何自动刷新 iframe angularjs?
【发布时间】:2015-11-30 03:39:14
【问题描述】:

这是代码(使用 Angularjs 和服务器运行 localhost )

index.html

<iframe src="{{Url}}" width="100%" height="500"></iframe>

app.js

$scope.Url = "http://0.0.0.0:8080/index.html";

需要每 30 秒刷新一次 怎么可能?

【问题讨论】:

    标签: html angularjs iframe


    【解决方案1】:

    我会使用 $timeout 功能。设置一个初始值,然后每 30 秒调用一次 reload 函数并增加一个查询字符串,这样就不会出现缓存问题。如果这不是问题,您可以删除 $scope.seed 内容。

    angular
        .module('app')
        .controller('UrlController', UrlController);
    
    UrlController.$inject = ['$scope', '$timeout'];
    
    function UrlController($scope, $timeout) {
    
        $scope.loadingCounter = 0;
    
        $scope.seed = getUtcDate().getTime();
        //adding the counter so the url reload isn't cached.
        $scope.baseUrl = 'http://0.0.0.0:8080/index.html';
    
        $scope.Url = 'http://0.0.0.0:8080/index.html';
    
        $scope.getReloadUrl = getReloadUrl;
        $scope.onReload = onReload;
    
        function onReload() {
            $scope.loadingCounter++;
            $scope.Url = $scope.getReloadUrl();
            //restart it again.
            $timeout($scope.onReload, 30000);
        }
    
        function getReloadUrl(){
            //create a unique query string
            var reloadId = $scope.seed + $scope.loadingCounter;
            return $scope.baseUrl + '?reloadId=' + reloadId;
        }
    
        //start it up
        $scope.onReload();
    
        //helper function 
        function getUtcDate() {
            var now = new Date();
            var nowUtc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
            return nowUtc;
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      你可以使用 Javascript 和 setInterval 来刷新 Source 的 src...

      <script>
      window.setInterval("reloadIFrame();", 30000);
      
      function reloadIFrame() {
       document.frames["frameNameHere"].location.reload();
      }
      </script>
      

      【讨论】:

      • 感谢您的回答
      • 但我正在等待 angularjs 的回答
      猜你喜欢
      • 2011-10-21
      • 2021-05-15
      • 2017-03-23
      • 2017-12-14
      • 2015-11-29
      • 2022-06-10
      • 2013-09-14
      • 2013-11-06
      相关资源
      最近更新 更多