4.0.4 • Published 7 years ago

ng-avatar v4.0.4

Weekly downloads
4
License
MIT
Repository
github
Last release
7 years ago

NgAvatar

This library is an Angular wrapper around the avatar-initials library by @MatthewCallis.

Usage

Component + Service

AppModule.ts

import { NgModule } from '@angular/core'
import { NgAvatarModule, AvatarService } from 'ng-avatar'
import { AppComponent } from 'app.component'

// Optional styles
import 'ng-avatar/avatar.scss'

@NgModule({
	declarations: [
		AppComponent
	],
	imports: [
		NgAvatarModule.forRoot()
	],
	providers: [
		AvatarService
	],
	bootstrap: [
		AppComponent
	]
})
export class AppModule {}

AppComponent

import { Component } from '@angular/core'

@Component({
	selector: 'my-app',
	template: `
		<img alt="" src="" id="avatar-image">
		<ng-avatar name="John Doe" email="john@johndoe.com" (onSuccess)="setImage($event)></ng-gravatar>
	`
})
export class AppComponent
{
	setImage(src:string)
	{
		document.getElementById('avatar-image').setAttribute('src', src);
	}
}

Just The Service

import { AvatarService } from 'ng-avatar'
import { Component } from '@angular/core'

@Component({
	selector: 'my-app',
	template: `
		<img alt="" src="" id="avatar-image">
		<button type="button" (click)="setImage()">Get Avatar</button>
	`
})
export class AppComponent
{
	constructor(private avatar:AvatarService) {}
	
	setImage()
	{
		let initials:string = this.avatar.Avatar('initials', 'John Doe'),
			gravatar:string = this.avatar.Avatar('gravatar', 'John Doe', 'john@johndoe.com');
		
		document.getElementById('avatar-image').setAttribute('src', initials);
	}
}

Run in Development

$ npm start

Build for NPM

$ npm run build