npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ng-onload

v0.8.0

Published

ngOnload directive for allowing callbacks from Angulars Scope to be executed on elements onload event.

Downloads

6,017

Readme

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.