在AngularJS耳目一新的iframe内容 [英] refreshing iframe contents in AngularJS

查看:164
本文介绍了在AngularJS耳目一新的iframe内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个应用程序,它是一部分Anugular及部分jQuery的。我通过加载在一个iFrame jQuery的内容分开来。

I am writing an application which is part Anugular and part jQuery. I am separating them by loading the jQuery content in an iFrame.

一旦某个事件(比如,在一个NG-点击),我需要刷新的iFrame。我的控制器包含以下code:

Upon a certain event (say, upon a ng-click), I need to refresh the iFrame. My Controller contains the following code:

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

和iFrame的是:

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

有人可以帮我一下吧。我可以发布更多code。如果需要的。

Can someone help me with it. I can post more code if the required.

推荐答案

由于圣保罗Scardine说,正确的方式做这将是通过一个指令事业的你不应该用控制器来操作DOM

As Paulo Scardine said, the right way to do it would be through a directive cause you shouldn't use controllers to manipulate 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();
                }
            });
        }
    };
}])

然后其可用于这样的:

Which could then be used like :

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

$scope.refreshIframe = function(){
    $scope.tab.refresh = true;
}

这篇关于在AngularJS耳目一新的iframe内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆