Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular, onLoad function on an iFrame

I have this iframe working with basic JavaScript:

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

Which triggers the method uploadDone(); when the content of the iframe has been loaded.

How do I do the same thing in Angular?. I want to call a function on the controller when the iframe loads, but I haven't seen a ng-onload so far.

like image 335
Jerome Ansia Avatar asked Apr 08 '13 14:04

Jerome Ansia


People also ask

Does iframe have onload?

If you want to run JavaScript when an iframe has finished loading, you can either: include code in the iframe tag's onload attribute, or. assign code to the iframe's onload event handler property.

How do you call a function after an iframe is loaded?

To call a JavaScript function when iframe finished loading, we can add an event listener for the load event. to add an iframe. to select the iframe with querySelector . Then we set the iframe.

How do I check if an iframe is loaded?

To check if iframe is loaded or it has a content with JavaScript, we can set the iframe's onload property to a function that runs when the iframe is loaded. document. querySelector("iframe"). onload = () => { console.

What is iframe in angular?

When the iframeSettings option is enabled, the Rich Text Editor creates the iframe element as the content area on control initialization. It is used to display and editing the content in content area. The editor will display only the body tag of a < iframe > document.


3 Answers

Commenting on a year old question. For cases where there are more than 1 iframes, I needed to bind "onload" events on to. I did this approach.

Directive

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

Controller

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

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

DOM

<div ng-controller="MyController">
    <iframe iframe-onload="iframeLoadedCallBack()" src="..."></iframe>
</div>
like image 92
Miguel Felipe Guillen Calo Avatar answered Oct 16 '22 13:10

Miguel Felipe Guillen Calo


try defining the function within controller as:

window.uploadDone=function(){
  /* have access to $scope here*/
}
like image 26
charlietfl Avatar answered Oct 16 '22 15:10

charlietfl


For anyone using Angular 2+, It's simply:

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

No global function, works with multiple iframe.

like image 29
Haijin Avatar answered Oct 16 '22 14:10

Haijin