Hello Devs, 

In this tutorial, we will learn Angular Add Custom Attribute to Element Example

In this section, you will do get and set custom attribute in angular 6, angular 7, angular 8 and angular 9 application:

Follow this step by step guide below. 


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



  selector: 'my-app',

  templateUrl: './app.component.html',

  styleUrls: [ './app.component.css' ]


export class AppComponent  {

  name = 'Angular';


  students = [

    {id:1, name:"Hardik"},

    {id:2, name:"Vimal"},

    {id:3, name:"Harshad"},




    var id = e.getAttribute('data-id');





<h1>angular add and get custom attribute to element - rathorji.in</h1>



  <li #studentID *ngFor="let student of students" [attr.data-id]="student.id">

    <button (click)="clickEvent(studentID)">{{ student.name }}</button>



May this example help you.