Skip to main content

Services and helpers

The services, functions and types that bootstrap-ng exports besides its components.

BsToaster

Shows toasts from code, without a toast in your template. The toasts stack in a fixed .toast-container that is added to the page the first time a toast is shown.

import { Component, inject } from '@angular/core';
import { BsButton, BsToaster } from 'bootstrap-ng';

@Component({
  selector: 'app-save',
  imports: [BsButton],
  template: `<button type="button" bsButton theme="primary" (click)="save()">Save</button>`,
})
export class Save {
  private readonly toaster = inject(BsToaster);

  protected save(): void {
    this.toaster.show({ title: 'Saved', body: 'Your changes were saved.' });
  }
}
NameTypeDescription
show(options)BsToastRefShows a toast and returns a reference with hide() and afterHidden.
clear()voidHides every toast.
setPlacement(placement)voidWhere the toasts stack on the screen. Defaults to 'bottom-end'.

See Toasts for every option, themes and templates with actions.

BsLiveAnnouncer

Reads a message out to screen reader users through visually hidden live regions that stay on the page. Use it for changes that happen without moving focus, such as search results updating. Toasts and the carousel already announce through it.

import { Component, inject, signal } from '@angular/core';
import { BsLiveAnnouncer } from 'bootstrap-ng';

@Component({ … })
export class Search {
  private readonly announcer = inject(BsLiveAnnouncer);
  protected readonly results = signal<string[]>([]);

  protected search(query: string): void {
    // …
    this.announcer.announce(`${this.results().length} results found`);
  }
}
NameTypeDescription
announce(message, politeness?)voidReads the message out. Politeness is 'polite' (the default) or 'assertive', which interrupts the screen reader and is meant for errors.
clear()voidClears pending and current announcements.

See Accessibility for why a region that stays on the page is announced more reliably than one added with its text.

BsIconRegistry

The icon libraries that bs-icon loads from, and a cache of the icons it has loaded. bootstrap (the default), font-awesome, material, modus and phosphor are built in and load from jsDelivr. Set the default library and add your own with provideBsIcons(), self-host a built-in set by passing a baseUrl to its factory, such as bootstrapIcons(), fontAwesomeIcons(), materialSymbols(), modusIcons() or phosphorIcons(), or bundle icons as SVG markup in a library’s icons. Bundled icons need no request and render on the server; each factory takes icons too, which are used before its files.

import { ApplicationConfig } from '@angular/core';
import { bootstrapIcons, provideBsIcons } from 'bootstrap-ng';
// Bundled into your app as text, so it shows without a request.
import logo from './logo.svg' with { loader: 'text' };

export const appConfig: ApplicationConfig = {
  providers: [
    provideBsIcons({
      // Icons without a library come from Font Awesome.
      defaultLibrary: 'font-awesome',
      libraries: {
        // Serve Bootstrap Icons from your own site (see the assets entry below).
        bootstrap: bootstrapIcons({ baseUrl: '/icons/bootstrap' }),
        // Any icon set with an SVG file per icon.
        lucide: {
          resolver: (name) => `https://cdn.jsdelivr.net/npm/[email protected]/icons/${name}.svg`,
        },
        // Icons bundled as SVG markup: <bs-icon library="app" name="logo" />.
        app: { icons: { logo } },
      },
    }),
  ],
};

To self-host, install the icon set’s npm package and copy its SVG files into your build with an assets entry in angular.json, then pass the output path as baseUrl. Use the package folder that holds the set’s variant folders: bootstrap-icons/icons, @fortawesome/fontawesome-free/svgs, @material-symbols/svg-400, @trimble-oss/modus-icons/dist or @phosphor-icons/core/assets, with **/*.svg as the glob for sets that have variants.

"assets": [
  { "glob": "**/*", "input": "public" },
  { "glob": "*.svg", "input": "node_modules/bootstrap-icons/icons", "output": "icons/bootstrap" }
]

To import an SVG file as text, as logo.svg above, declare its type once in your app.

// src/svg.d.ts: types for importing SVG files as text.
declare module '*.svg' {
  const content: string;
  export default content;
}
NameTypeDescription
register(name, library)voidAdds an icon library, or replaces the one with the same name.
addIcons(library, icons)voidAdds icons, as SVG markup by name, to a library, creating it when it does not exist. They show without a request and render on the server.
get(name)BsIconLibrary | undefinedReturns the library registered under the name.
load(url)Promise<SVGSVGElement>Fetches an SVG once, removes scripts and event handlers, and returns a copy.
parse(markup)SVGSVGElementParses SVG markup, cleaned like a loaded SVG, and returns a copy. Throws when it is not an SVG.
defaultLibraryWritableSignal<string>Name of the library that icons without a library use.

See Icon for every built-in library and its variants.

Helper functions

bootstrap-ng uses these internally. They are exported for components that you build on top of it.

NameTypeDescription
themeClass(theme)stringReturns the .theme-* class for a BsTheme, such as 'theme-danger', or an empty string when the theme is null.
waitForTransition(element)Promise<void>Resolves when the element's CSS transition ends, at once when it has none, and after a fallback timer if transitionend never fires.
getTransitionDuration(element)numberThe element's first transition duration plus delay, in milliseconds.
uniqueId(prefix)stringReturns an id that is unique on the page, such as 'bs-collapse-3'.

For example, to emit an event after a fade-out finishes, as bootstrap-ng's own hidden and closed outputs do:

protected async close(): Promise<void> {
  this.element.classList.remove('show');
  await waitForTransition(this.element);
  this.closed.emit();
}

Types

NameTypeDescription
BsThemestring union'primary' | 'accent' | 'success' | 'danger' | 'warning' | 'info' | 'inverse' | 'secondary'
BsPlacementstring unionWhere a tooltip, popover or menu opens: 'top', 'bottom', 'left', 'right', 'start' or 'end', optionally followed by '-start' or '-end'.

BOOTSTRAP_NG

An array of every component and directive, to add to a component's imports while prototyping. Import single components in production code so unused ones are removed from your bundle. See Conventions.