Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iFrame上的角度、加载函数_Iframe_Angularjs - Fatal编程技术网

iFrame上的角度、加载函数

iFrame上的角度、加载函数,iframe,angularjs,Iframe,Angularjs,我有一个使用基本JavaScript的iframe: <iframe id="upload_iframe" name="upload_iframe" onLoad="uploadDone();"></iframe> 它触发方法uploadDone()当iframe的内容已加载时 我如何在角度上做同样的事情?。我想在iframe加载时调用控制器上的函数,但到目前为止我还没有看到ng onload。尝试将控制器中的函数定义为: window.uploadDone=fun

我有一个使用基本JavaScript的iframe:

<iframe id="upload_iframe" name="upload_iframe" onLoad="uploadDone();"></iframe>

它触发方法
uploadDone()当iframe的内容已加载时


我如何在角度上做同样的事情?。我想在iframe加载时调用控制器上的函数,但到目前为止我还没有看到
ng onload

尝试将控制器中的函数定义为:

window.uploadDone=function(){
  /* have access to $scope here*/
}

评论一个一年前的问题。 对于有多个iframe的情况,我需要将“onload”事件绑定到。 我就是这样做的

指示

APP.directive('iframeOnload', [function(){
return {
    scope: {
        callBack: '&iframeOnload'
    },
    link: function(scope, element, attrs){
        element.on('load', function(){
            return scope.callBack();
        })
    }
}}])
控制器

APP.controller('MyController', ['$scope', function($scope){

    $scope.iframeLoadedCallBack = function(){
        // do stuff
    }
}]);
多姆



对于任何来到这里的人来说,这个插件是解决这个问题的完美方案。它不会污染全局名称空间,并且当您只想调用一个简单的作用域函数时,不需要创建一次性指令。

对于任何使用Angular 2+的人, 很简单:

<iframe (load)="uploadDone()"></iframe>


没有全局函数,可与多个iframe一起使用。

对于那些动态注入iframe的函数,可以执行以下操作

html

<div #iframeContainer></div>

是否也有传递角度参数的选项?例如
code
这样做的缺点是它将全局函数与角度代码混合在一起。这是可行的,但可能会令人困惑。例如,“其他阅读代码的人会期望在控制器中找到它吗?它会与其他任何东西冲突吗?”很好,谢谢这个指令。我更改了指令中的回调调用:
returnscope.callback(元素)scope.$on('$destroy',function(){element.off('load');})
ngOnload只会返回与Angular 1.5.x的contentWindow相关的未定义。您在这个版本的AngularJS上使用过它吗?hajjin我如何显示加载程序直到上传完成您可以拥有一个变量showLoader,并根据它的值显示加载程序。加载开始后将其设置为true。并在uploadDone内部将其设置为false。
<div #iframeContainer></div>
@Component({
  selector: 'app-iframe-onload',
  templateUrl: './iframe-onload.component.html'
})
export class IframeOnload implements AfterViewInit {

@ViewChild('iframeContainer') iframeContainer: ElementRef;

ngAfterViewInit(): void {
  this.injectIframe();
}

private injectIframe(): void {
  const container = this.iframeContainer.nativeElement;
  const iframe = document.createElement('iframe');
  iframe.setAttribute('width', '100%');
  iframe.setAttribute('src', 'https://example.com/');
  iframe.setAttribute('height', 'auto');
  iframe.setAttribute('frameBorder', '0');
  iframe.addEventListener('load', this.iframeOnLoadtwo);
  container.appendChild(iframe);

}

public iframeOnLoadtwo(): void {
  console.log('iframe loaded...');
}

}