GithubHelp home page GithubHelp logo

dmitry84 / ng-onload Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mikaturunen/ng-onload

0.0 1.0 0.0 73 KB

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

License: MIT License

JavaScript 89.43% HTML 10.57%

ng-onload's Introduction

ng-onload

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.


Installation

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="www.foobar.com" ng-onload="callbackFromScope(contentLocation)"></iframe>

Usage

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

application.js:

// Creating magical Angular application to solve problem X
angular
    .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!");
        };
    });

index.html:

<!doctype html>
    <head>
        <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>
    </head>
    <body>
        <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="http://some.location.com/somewhere" ng-onload="hello(contentLocation)"></iframe>
        </div>
    </body>
</html>

Developing

Prerequisites:

  • Have yarn installed globally (npm i -g yarn)
$ yarn install    # install dependencies
$ npm run build   # builds the release and makes sure everything is good

Other

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: https://gist.github.com/mikaturunen/f0b45def06bc83ccea9e

FAQ

1

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.

ng-onload's People

Contributors

mikaturunen avatar mtpultz avatar nillis avatar piccirello avatar rbournissent avatar wbruno avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.