小编典典

在AngularJS中刷新iframe内容

angularjs

我正在编写一个应用程序,它是Angular和jQuery的一部分。我通过在iFrame中加载jQuery内容来分离它们。

在某个事件(例如,单击ng)时,我需要刷新iFrame。我的控制器包含以下代码:

$scope.refreshIframe = function() { //refresh the iFrame with id "anIframe" };

而iFrame是:

<iframe id="anIframe" src="myUrl"></iframe>

阅读 429

收藏
2020-07-04

共1个答案

小编典典

正如Paulo
Scardine所说,正确的方法是通过指令,因为您不应该使用控制器来操纵DOM

这样的事情可以做:

.directive('refreshable', [function () {
    return {
        restrict: 'A',
        scope: {
            refresh: "=refreshable"
        },
        link: function (scope, element, attr) {
            var refreshMe = function () {
                element.attr('src', element.attr('src'));
            };

            scope.$watch('refresh', function (newVal, oldVal) {
                if (scope.refresh) {
                    scope.refresh = false;
                    refreshMe();
                }
            });
        }
    };
}])

然后可以这样使用:

<iframe refreshable="tab.refresh"></iframe>

和:

$scope.refreshIframe = function(){
    $scope.tab.refresh = true;
}
2020-07-04