Binds Angular.js scope function to given HTML elements onload event; for example, iframe.

Module License: MIT

Added by: Sam Deering





Stars: 30

Watchers: 30

Forks: 9

Module Description


Binds Angular.js scope function to given HTML elements onload event; for example, iframe.

There is no direct way of binding angular to elements onload event as commonly the HTML elements onload="" attribute looks into the JavaScripts global name space (window.*) which is a big no-no. It used to be the norm back in the day of how to do things but with modern frameworks like AngularJs and the such the approach has changed a lot.

This is just one example (and by no means the only way) of how to get Angular behave nicely with HTML elements onload event. As <iframe onload="test()"> looks into window.test for a callback we need to bound the onload event to look into provided angular scope for the callback.


bower install ng-onload --save

Usage - TL;DR

Super short version of how to use the ng-load directive in your Angular application.

// Include it in Angular as per
angular.module("magicalRocketUnicornApplication", [ "ngOnload" ])

// After that it's ready to rock - Usage in HTML
// Please note the use of 'contentLocation' variables, it's a named property and this way it provides the 'location' of the IFrame to the callback
<iframe src="" ng-onload="callbackFromScope(contentLocation)"></iframe>


A longer example with some other Angular content included that you'll normally have in your Angular.js application.


// Creating magical Angular application to solve problem X
    .module("magicalRocketUnicornApplication", [ "ngOnload" ])
    .controller("UnicornController", function($scope) {
        // This is the scope callback we are going to call when the elements
        // onload event triggers

        $scope.hello = function(contentLocation) {
            // contentLocation === iframe.contentWindow.location
            // it's undefined when contentWindow cannot be found from the bound element
            alert("Hello world!");


<!doctype html>
        <meta charset="utf-8">
        <script src="../bower_components/angular/angular.min.js"></script>
        <script src="../bower_components/ng-load/release/ng-onload.min.js"></script>
        <script src="application.js"></script>
        <div ng-app="magicalRocketUnicornApplication" ng-controller="UnicornController">
            <!-- Notice the use of 'contentLocation' if you want to use the named parameters to get location property -->
            <iframe src="" ng-onload="hello(contentLocation)"></iframe>


You can find and example .html and how it's used inside the /test directory. There currently are no specific unit tests available but feel free to contribute.

Please note that the code is written in ES6 JavaScript which is then compiled to ES5 compatible JavaScript and the compiled + uglified result can be found from /release/ng-onload.min.js.

This small angular directive started from my old GIST:



Q. iframe location does not work! It's undefined!

A. It does. It has been fixed and it explicitly takes advantage of named properties and that's some dark magic right there. Look at the test/index.test.html to see it in action.

Module stats last updated: 2017-02-24 16:00:03

Disclaimer: Some data on this page may have been gathered from the authors GitHub respository. If you see any mistakes or outdated information please let us know. Thanks!