Getting started with Angular Badge component

9 Dec 20243 minutes to read

The following section explains the steps required to create a simple Badge component using styles and its basic usage.

Setting up angular project

Angular provides the easiest way to set angular CLI projects using Angular CLI tool.

Install the CLI application globally to your machine by using following command.

npm install -g @angular/cli

Create a new angular application

ng new syncfusion-angular-app

Navigate to the created project folder by using following command.

cd syncfusion-angular-app

Refer Syncfusion Angular Getting Started section to know more about setting up angular-cli project.

Dependencies

The Badge component is pure CSS component which doesn’t need specific dependencies to render.

|-- @syncfusion/ej2-notifications

Installing Syncfusion notifications Package

Syncfusion packages are distributed in npm as @syncfusion scoped packages. You can get all the Angular Syncfusion package from npm link.

Currently, Syncfusion provides two types of package structures for Angular components,

  1. Ivy library distribution package format
  2. Angular compatibility compiler(Angular’s legacy compilation and rendering pipeline) package.

Ivy library distribution package

Syncfusion Angular packages(>=20.2.36) has been moved to the Ivy distribution to support the Angular Ivy rendering engine and the package are compatible with Angular version 12 and above. To download the package use the below command.

Add @syncfusion/ej2-angular-notifications package to the application.

npm install @syncfusion/ej2-angular-notifications --save

Angular compatibility compiled package(ngcc)

For Angular version below 12, you can use the legacy (ngcc) package of the Syncfusion Angular components. To download the ngcc package use the below.

Add @syncfusion/ej2-angular-notifications@ngcc package to the application.

npm install @syncfusion/ej2-angular-notifications@ngcc --save

To mention the ngcc package in the package.json file, add the suffix -ngcc with the package version as below.

@syncfusion/ej2-angular-notifications:"20.2.38-ngcc"

Note: If the ngcc tag is not specified while installing the package, the Ivy Library Package will be installed and this package will throw a warning.

Adding style sheet to the application

To render the Badge component, import the Badge and its dependent component’s styles as given below in [src/styles.css].

@import '../node_modules/@syncfusion/ej2-base/styles/material.css';
@import '../node_modules/@syncfusion/ej2-notifications/styles/material.css';
@import '../node_modules/@syncfusion/ej2-angular-notifications/styles/material.css';

Note: To refer the combined component styles, use Syncfusion CRG (Custom Resource Generator) in your application.

Add Badge into application

Modify the template in app.component.ts file to render the Badge component.

[src/app/app.component.ts]

import { Component } from '@angular/core';

@Component({
imports: [
        
    ],
    standalone: true,
    selector: 'app-root',
    template: `<div id='element'><h1>Badge Component <span class="e-badge">New</span></h1></div>`
})

export class AppComponent {}

Run the application

Run the application in the browser using the following command.

npm start

The following example shows a basic badge component.

import { NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'



import { Component } from '@angular/core';

@Component({
imports: [
        
    ],


standalone: true,
    selector: 'app-root',
    styleUrls: ['./app.component.css'],
    template: `<div id='element'><h1>Badge Component <span class="e-badge">New</span></h1></div>`
})

export class AppComponent {}
#element {
    display: flex;
    width: 400px;
    margin: auto;
    border: 1px solid #dddddd;
    border-radius: 3px;
    justify-content: center;
  }
import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));

You can refer to our Angular Badge feature tour page for its groundbreaking feature representations. You can also explore our Angular Badge example to knows how to present and manipulate data.