core/alert/alert.service.ts

Description

A service which allows to display alerts.

Extends

StateService

Metadata

Relationships

Used by

No results matching.

Depends on

AlertService

Alert service provides methods for adding different types of alerts.

Usage examples

Basic usage

Simple methods for basic usage. Each method can be used with optional details parameter for showing details.

Example :
  import { AlertService } from '@c8y/ngx-components';
  (...)
  constructor(protected alertService: AlertService) {}
  (...)
  showAlerts () {

    // simple usage
    this.alertService.success( 'Success message' );
    this.alertService.danger( 'Danger message' );
    this.alertService.info( 'Info message' );
    this.alertService.warning( 'Warning message' );

    this.alertService.addByText('danger', 'Danger message');

    // alert with details
    this.alertService.danger ( 'Error message', 'Details of error' );
  }

Custom timeout

Alert timeout property is responsible for hiding alert after defined (in milliseconds) time.

Example :
  import { Alert, AlertService } from '@c8y/ngx-components';
  (...)
  constructor(protected alertService: AlertService) {}
  (...)
  showAlert () {
    const alert: Alert = {
      type: 'danger',
      text: 'Danger message',
      timeout: 5000 // Alert will disappear after 5 seconds
    }
    this.alertService.add( alert );
  }

Using html/TemplateRef for presenting alert

Alert text can be defined as HTML, allowHtml property needs to be set as true.

Example :
  import { Alert, AlertService } from '@c8y/ngx-components';
  (...)
  constructor(protected alertService: AlertService) {}
  (...)
  showAlert () {
    const alert: Alert = {
      type: 'danger',
      text: '<div>My message</div>',
      allowHtml: true
    }
    this.alertService.add( alert );
  }

Using callbacks

Alerts provide two callbacks:

  • onDetail: when "show detail" button are clicked
  • onClose: when alert is closed by user
Example :
  import { Alert, AlertService } from '@c8y/ngx-components';
  (...)
  constructor(protected alertService: AlertService) {}
  (...)
  showAlert () {
    const onDetailCallback = () => { console.log('On detail button clicked') };
    const onCloseCallback = () => { console.log('On close button clicked') };
    const alert: Alert = {
      type: 'danger',
      text: '<div>My message</div>',
      onDetail: onDetailCallback,
      onClose: onCloseCallback
    }
    this.alertService.add( alert );
  }

results matching ""

    No results matching ""