Skip to content
View as Markdown

Angular avatar library: using DiceBear with Angular ​

DiceBear fits into Angular components through Signals (Angular 17+) or the OnChanges lifecycle hook, with the JavaScript library for client-side generation or the HTTP API as a plain <img> source without any dependency.

With the JS library ​

typescript
import { Component, input, computed } from '@angular/core';
import { Style, Avatar } from '@dicebear/core';
import lorelei from '@dicebear/styles/lorelei.json' with { type: 'json' };

const style = new Style(lorelei);

@Component({
  selector: 'app-avatar',
  template: `<img [src]="avatarUrl()" alt="Avatar" />`,
})
export class AvatarComponent {
  seed = input('Alice');

  avatarUrl = computed(() =>
    new Avatar(style, {
      seed: this.seed(),
      size: 128,
      // ... other options
    }).toDataUri(),
  );
}
typescript
import { Component, Input, OnChanges } from '@angular/core';
import { Style, Avatar } from '@dicebear/core';
import lorelei from '@dicebear/styles/lorelei.json' with { type: 'json' };

const style = new Style(lorelei);

@Component({
  selector: 'app-avatar',
  standalone: true,
  template: `<img [src]="avatarUrl" alt="Avatar" />`,
})
export class AvatarComponent implements OnChanges {
  @Input() seed: string = 'Alice';
  avatarUrl: string = '';

  ngOnChanges() {
    this.avatarUrl = new Avatar(style, {
      seed: this.seed,
      size: 128,
      // ... other options
    }).toDataUri();
  }
}

With the HTTP API ​

typescript
import { Component, input, computed } from '@angular/core';

@Component({
  selector: 'app-avatar',
  template: `<img [src]="avatarUrl()" alt="Avatar" />`,
})
export class AvatarComponent {
  seed = input('Alice');

  avatarUrl = computed(() => {
    const url = new URL('https://api.dicebear.com/11.x/lorelei/svg');
    url.searchParams.set('seed', this.seed());
    url.searchParams.set('size', '128');
    // ... other options
    return url.href;
  });
}
typescript
import { Component, Input, OnChanges } from '@angular/core';

@Component({
  selector: 'app-avatar',
  standalone: true,
  template: `<img [src]="avatarUrl" alt="Avatar" />`,
})
export class AvatarComponent implements OnChanges {
  @Input() seed: string = 'Alice';
  avatarUrl: string = '';

  ngOnChanges() {
    const url = new URL('https://api.dicebear.com/11.x/lorelei/svg');
    url.searchParams.set('seed', this.seed);
    url.searchParams.set('size', '128');
    // ... other options
    this.avatarUrl = url.href;
  }
}