Skip to content

Instantly share code, notes, and snippets.

@mahsaml90
Last active October 2, 2020 15:29
Show Gist options
  • Save mahsaml90/a908fc7b3085927119db4f9c606d85f6 to your computer and use it in GitHub Desktop.
Save mahsaml90/a908fc7b3085927119db4f9c606d85f6 to your computer and use it in GitHub Desktop.
Angular 2 Directive to use with Jdenticon

How to use

  1. install Jdenticon using npm install jdenticon --save
  2. import this directive into your intended module and add it to declarations.
  3. then use it in your html file like this:
<svg width="50" height="50" [identiconHash]="hash">
  Fallback text for browsers not supporting inline svg
</svg>
import { Directive, ElementRef, Input, OnChanges, Renderer2, SimpleChanges } from '@angular/core';
import jdenticon from 'jdenticon';
@Directive({ selector: '[identiconHash]' })
export class IdenticonHashDirective implements OnChanges {
@Input() identiconHash: string;
constructor(private el: ElementRef, private renderer: Renderer2) {}
ngOnChanges(changes: SimpleChanges) {
jdenticon.update(this.el.nativeElement, this.identiconHash);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment