How to implement 'add tag' functionality in an Angular 9 app ? Last Updated : 16 Feb, 2023 Comments Improve Suggest changes Like Article Like Report 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: Angular material library provides mat-chip, which can be used to implement this functionality.We can use it inside a form field to take input from user and update our tags list.Once user has finished adding tags we can save the tag list.Now we have our tag list, we can use it any way that we want. Step by step implementation: For the component class: Import MatChipInputEvent from @angular/material/chips to handle the tags input event.Import COMMA, ENTER from @angular/cdk/keycodes to add the separator keys.Create a list which will contain all the tags entered by user.Create your custom add and remove method to add and remove tags.Code for the component: javascript 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: Create a form field which will take input and display the list of tags.There are some parameters:1.matChipInputFor: it takes the id of form field, from which we will take the input of tags.2.matChipInputSeparatorKeyCodes: It takes the value of keys which will be used as separator.3.matChipInputTokenEnd: As soon as user press separator key, this will contain the last entered tag, and we can update the tag list by our custom add method.To remove a particular tag, add a mat-icon with matChipRemove directive.Code for the HTML view: html <!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> Now in the main component include this 'add-tags' component. javascript 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 { } We have successfully implemented add tags functionality. Output: Mat-chips over the form field represents the tags entered by the user. Create Quiz Comment V vaibhav19verma Follow 0 Improve V vaibhav19verma Follow 0 Improve Article Tags : Web Technologies AngularJS AngularJS-Misc Explore AngularJS BasicsAngularJS Tutorial 5 min read Introduction to AngularJS 4 min read Angular CLI | Angular Project Setup 3 min read AngularJS Expressions 2 min read AngularJS Modules 3 min read AngularJS ng-model Directive 4 min read AngularJS Data Binding 4 min read AngularJS Controllers 3 min read AngularJS | Scope 2 min read AngularJS Services 4 min read AngularJS | AJAX - $http 3 min read AngularJS | Tables 2 min read AngularJS Select Boxes 2 min read AngularJS SQL 3 min read AngularJS HTML DOM 2 min read AngularJS Events 3 min read AngularJS | Forms 3 min read AngularJS Form Validation 3 min read AngularJS | API 2 min read AngularJS and W3.CSS 2 min read AngularJS Includes 3 min read AngularJS Animations 1 min read AngularJS | Application 3 min read AngularJS DirectivesAngularJS Directives 9 min read AngularJS ng-app Directive 1 min read AngularJS ng-bind Directive 2 min read AngularJS ng-bind-html Directive 2 min read AngularJS ng-bind-template Directive 2 min read AngularJS ng-blur Directive 1 min read AngularJS ng-change Directive 2 min read AngularJS ng-checked Directive 2 min read AngularJS ng-class Directive 2 min read AngularJS ng-class-even Directive 2 min read AngularJS ng-class-odd Directive 2 min read AngularJS ng-click Directive 2 min read AngularJS ng-cloak Directive 2 min read AngularJS ng-controller Directive 2 min read AngularJS Directives Complete Reference 2 min read AngularJS FiltersAngularJS | Filters 7 min read AngularJS currency Filter 2 min read AngularJS | date Filter 2 min read AngularJS filter Filter 3 min read AngularJS json Filter 2 min read AngularJS limitTo Filter 2 min read AngularJS lowercase Filter 1 min read AngularJS number Filter 1 min read AngularJS orderBy Filter 4 min read AngularJs uppercase Filter 1 min read AngularJS Converting FunctionsAngularJS angular.lowercase() Function 2 min read AngularJS angular.uppercase() Function 1 min read AngularJS angular.forEach() Function 1 min read AngularJS Comparing FunctionsAngularJS angular.isArray() Function 2 min read AngularJS angular.isDate() Function 2 min read AngularJS angular.isDefined() Function 2 min read AngularJS angular.isElement() Function 2 min read AngularJS angular.isFunction() Function 2 min read AngularJS angular.isNumber() Function 2 min read AngularJS angular.isObject() Function 2 min read AngularJS | angular.isString() Function 1 min read AngularJS angular.isUndefined() Function 2 min read AngularJS angular.equals() Function 2 min read AngularJS angular.toJson() Function 2 min read AngularJS QuestionsHow to bundle an Angular app for production? 4 min read How to add many functions in one ng-click directive? 2 min read How to directly update a field by using ng-click in AngularJS ? 3 min read How to Add Dynamic Options for Multiple Selects Inside ng-repeat Directive ? 3 min read How to detect when an @Input() value changes in Angular? 3 min read How to open popup using Angular and Bootstrap ? 2 min read How to reload or re-render the entire page using AngularJS? 2 min read How to add input fields dynamically on button click in AngularJS ? 2 min read How to Create Button Dynamically with Click Event in Angular ? 2 min read How to use jQuery in Angular ? 2 min read AngularJS Examples 2 min read Like