Open In App

How to implement ‘add tag’ functionality in an Angular 9 app ?

Angular makes it very easy to implement almost every functionality. In this article, we will learn how to implement add tag functionality in your angular app. Adding tags have applications in several areas like music apps, online shopping apps, etc. By using this functionality we can filter the results of a search, according to the need of the user.

Approach:



Step by step implementation:

For the component class:






import {Component} from '@angular/core';
import {COMMA, ENTER} from '@angular/cdk/keycodes';
import {MatChipInputEvent} from '@angular/material/chips';
 
@Component({
  selector: 'add-tags',
  templateUrl: 'tags.html',
  styleUrls: ['tags.css'],
})
export class addTags {
 
/*Set the values of these properties
    to use them in the HTML view.*/
 
  visible = true;
  selectable = true;
  removable = true;
   
/*set the separator keys.*/
 
  readonly separatorKeysCodes: number[] = [ENTER, COMMA];
 
/*create the tags list.*/
 
  Tags: string[] = [];
   
/*our custom add method which will take
    matChipInputTokenEnd event as input.*/
  add(event: MatChipInputEvent): void {
 
/*we will store the input and value in local variables.*/
 
    const input = event.input;
    const value = event.value;
 
    if ((value || '').trim()) {
     
 /*the input string will be pushed to the tag list.*/
  
      this.Tags.push(value);
    }
 
    if (input) {
 
/*after storing the input we will clear the input field.*/
 
      input.value = '';
    }
  }
 
/*custom method to remove a tag.*/
 
  remove(tag: string): void {
    const index = this.Tags.indexOf(tag);
 
    if (index >= 0)
    {
 
/*the tag of a particular index is removed from the tag list.*/
 
      this.Tags.splice(index, 1);
    }
  }
}

For the HTML view:




<!DOCTYPE html>
<html>
    <head>
        <title>tutorial</title>
    </head>
    <body>
        <mat-form-field class="input">
            <!--this contains the list of tags-->
 
            <mat-chip-list #taglist>
                <!--set the properties for the tags-->
 
                <mat-chip selected color="primary"
                          *ngFor="let Tag of Tags"
                          [selectable]="selectable"
                          [removable]="removable"
                          (removed)="remove(Tag)">
                    {{Tag}}
                    <!--add icon with matChipRemove
                           directive to remove any tag-->
 
                    <mat-icon matChipRemove
                              *ngIf="removable">cancel
                  </mat-icon>
                </mat-chip>
                <input placeholder="Add Tags"
                       [matChipInputFor]="taglist"
              [matChipInputSeparatorKeyCodes]="separatorKeysCodes"
                       (matChipInputTokenEnd)="add($event)" />
            </mat-chip-list>
        </mat-form-field>
    </body>
</html>




import {Component} from '@angular/core';
 
@Component({
  selector: 'app-root',
  template: `
  <div style="align-items: center;">
  <app-test></app-test>
  </div>
  `,
  styleUrls: ['main.css'],
})
 
export class AppComponent {
 
}

Output: Mat-chips over the form field represents the tags entered by the user.


Article Tags :