ng-src
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Build Status

NPM

NgSrc

Small Angular directive that loads images using XHR and exposes the request.

Usefull for cases when we need to get additional information like headers set by the server.

The image is requested from the server as arraybuffer and a blob is created from the response. A local url is created with createObjectURL and this is set as src for the image tag.

Installing

Use npm or yarn to install

npm install ng-src --save

or

yarn add ng-src --prod

Using

Import NgSrcModule and add it to the imports array of your app module

import { NgSrcModule } from 'ng-src';

@NgModule({
  imports: [
    NgSrcModule,
  ],
})
export class AppModule { }

You now can use the ngSrc directive on image tags inside your application

 <img [ngSrc]="'http://localhost:4200/demo/assets/cat300.jpg'"
      (ngOnLoad)="exampleCallback($event)">

The directive exposes ngOnLoad event that is triggered when the image is loaded and gets as argument the XMLHttpRequest object. One can use this to get any additional information required like the headers

  exampleCallback(request: XMLHttpRequest) {
    console.log(request.getAllResponseHeaders());
  }

Contributing

Clone this repository

git clone https://github.com/adrianfaciu/ngsrc

And install all dependencies

yarn install

or

npm install

Source code is inside src/ng-src and a demo application is found inside src/demo.

Scripts

  • build:lib - build and bundle the lib package using ng-packar
  • serve:demo - serve the demo application
  • test - execute the tests
  • e2e - execute end to end tests
  • lint - run the linter on the source code

All these scripts can be used with npm run scriptName or yarn scriptName.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

  • Adrian Faciu - Initial work

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.0.148latest

Version History

VersionDownloads (Last 7 Days)Published
1.0.148
1.0.04
0.0.16

Package Sidebar

Install

npm i ng-src

Weekly Downloads

12

Version

1.0.1

License

MIT

Last publish

Collaborators

  • adrian.faciu