Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ng2-src-fallback

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ng2-src-fallback

Angular directive for images fallbacks with the capability of generating On-The-Fly fallbacks

  • 1.0.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

NG2 SRC FALLBACK

An Angular directive that substitutes the primary image with a fallback should it fail to load.

Features

  • Default fallback - If at first you don't succeed, provide a fallback image to [srcFallback] input
  • On-The-Fly Fallback creation - If for some reason you don't have an image to set as fallback, check the [customFallbackConfig]

Install

npm i --save ng2-src-fallback

Basic Usage

Add the Ng2SrcFallbackModule module to your module's imports

import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {AppComponent} from './app.component';
import {Ng2SrcFallbackModule} from 'ng2-src-fallback';

@NgModule({
	imports: [
		BrowserModule,
		Ng2SrcFallbackModule
	],
	declarations: [AppComponent],
	bootstrap: [AppComponent]
})

export class AppModule {}

Then add it to your component

import {Component} from '@angular/core';

@Component({
  	selector: 'neat-app',
  	template: `
	  	<img
	  		src="can_of_beans.png"
			ng2-src-fallback
			[srcFallback]="fallbackImgURL">
	`
})

export class AppComponent {
  	public fallbackImgURL = 'http://placehold.it/200x200';
}

[customFallbackConfig]

Let's say, for some reason, you don't have an image you can use as fallback. Instead of providing a value to the [srcFallback] input, you can use the [customFallbackConfig] to generate 'On-The-Fly' custom placeholder images.

ICustomFallbackConfig

NameTypeRequiredDescription
heightString (numeric value and CSS unit)yesHeight of the generated Image
widthString (numeric value and CSS unit)yesWidth of the generated Image
bgColourStringnoBackground colour of the generated Image (String)
textSizeAndFontString (CSS font property syntax)noStyling of the Text inside the generated Image
textColourStringnoColour of the Text inside the generated Image
textContentStringnoThe text you want to display inside the generated Image
textAlignCanvasTextAlignnoThe alignment of the text you want to display inside the generated Image
textBaselineCanvasTextBaselinenoThe Baseline alignment of the text you want to display inside the generated Image
mimeTypeString: 'jpeg' | 'png'noThe mime type of the generated Image

Usage

Add the Ng2SrcFallbackModule module to your module's imports

import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {AppComponent} from './app';
import {Ng2SrcFallbackModule} from 'ng2-src-fallback';

@NgModule({
	imports: [
		BrowserModule,
		Ng2SrcFallbackModule
	],
	declarations: [AppComponent],
	bootstrap: [AppComponent]
})

export class AppModule {}

Then add it to your component and provide a configuration object to the [customFallbackConfig] input

import {Component} from '@angular/core';
import {ICustomFallbackConfig} from 'ng2-src-fallback/custom-fallback-config';

@Component({
  	selector: 'neat-app',
  	template: `
	  	<img
	  		src="can_of_beans.png"
			ng2-src-fallback
			[customFallbackConfig]="customFallbackConfig">
	`
})

export class AppComponent {
	public customFallbackConfig: ICustomFallbackConfig = {
		height: '200px',
		width: '200px',
		bgColour: 'black',
		textSizeAndFont: '20px sans-serif',
		textColour: 'white',
		textContent: '200x200',
		textAlign: 'center',
		textBaseline: 'middle',
		mimeType: 'jpeg'
	};
}

Events

(wasLoaded)

ng2-src-fallback exposes a (wasLoaded) event that emits when either the src or srcFallback is loaded. To determine whether the original source or the fallback was loaded, add a param to your callback for the (wasLoaded) event.

Example:

First add callback to your component

wasLoaded(isFallback: boolean) {
	// do something based on 'isFallback'
	// or console.log it, don't just take my word
}

and then bind it to the wasLoaded event

<img
	src="a_cat.png"
	ng2-src-fallback
	[srcFallback]="'picture2.png'"
	(wasLoaded)="wasLoaded($event)">

License

MIT © Jaime 'Gondola' Oliveira

Keywords

FAQs

Package last updated on 17 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc