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

ng2-src-directive

v0.0.10

Published

An Angular2 src directive for any component

Downloads

39

Readme

ng2-src-directive

An Angular2 directive for fetching the source of an element from a remote.

Source Directive

The Source directive fetches the url provided and sets the innerHtml of the element to which it is attached to the text of the response.

Usage

In your component that will use src import the Source directive. Include Source in the component's directives array. Add the src attribute to an element inside your template.

import {Source} from 'ng2-src-directive/src';

@Component({
  selector: 'my-component'
  directives: [Source],
  template: [`
    <p src="my/remote/source.html"></p>
    <div [src]="mySource"></div>
  `]
})
export class MyComponent {
  mySource: string = 'some/other/source.txt';
  ...
}

Lifecycle Events

Lifecycle hooks are provided to allow your component to respond to source events with more granularity. The available hooks are:

  • sourceChanged(url: string): The value of the src attribute has changed, kicking off validation and fetching of the remote resource.
  • sourceLoading(url: string): The src attribute has been validated (not null) and the resource is being fetched
  • sourceError(string): An error occured while fetching the file, such as file not found.
  • sourceReceived(Response): The source was fetched successfully. the Response is the result from the angular2 Http get. If a sourceReceived implementation is provided on your component the source directive will no longer automatically set the innerHtml of the element to the reponse text. It is your responsibility and prerogative to handle the response.

The hooks each have a recommended corresponding interface that can be implemented by the component class for type checking. They are:

  • OnSourceChanged
  • OnSourceLoading
  • OnSourceError
  • OnSourceReceived

Usage

In your component that will use src import the Source directive, optionally one or more source interfaces, and the Response you will receive. Include Source in the component's directives array. Have your component implement the interface(s) if you want type checking.

import {Component} from 'angular2/core';
import {
  Source,
  OnSourceChanged,
  OnSourceLoading,
  OnSourceError,
  OnSourceReceived,
  Response
} from 'ng2-src-directive/src';

@Component({
  selector: 'my-component'
  directives: [Source],
  ...
})
export class MyComponent implements OnSourceChanged,
                                    OnSourceLoading,
                                    OnSourceError,
                                    OnSourceReceived {
  
}

Add the src attribute inside your template.

...
template: [`
  <p src="my/remote/source.html"></p>
`]

Implement any method on your component.

export class MyComponent implements OnSourceChanged,
                                    OnSourceLoading,
                                    OnSourceError,
                                    OnSourceReceived {
  
  constructor(private _el: ElementRef) { }

  sourceChanged(url: string) {
    console.log('Source changed to ' + url);
  }

  sourceChanged(url: string) {
    console.log('Source loading from ' + url);
  }

  sourceError(error: string) {
    console.error(error);
  }

  sourceReceived(res: Response) {
    this._el.nativeElement.innerHtml = res.text();
  }

}

Debounce Time

The Source directive automatically throttles http requests it sends out on src changes to prevent overwhelming the server. The default waiting time is 200 ms. There are two ways to change this.

  1. Set the debounceTime attribute to a number of milliseconds on an element:
<p src="remote/source.html" debounceTime="100"></p>
  1. Provide SourceDebounceTime with a number of milliseconds:
import {Component, provide} from 'angular2/core'
import {Source, SourceDebounceTime} from 'ng2-src-directive/src';

@Component({
  ...
  providers: [
    provide(SourceDebounceTime, {useValue: 100})
  ]
})

SourceDebounceTime can be provided at any level of the app.