Hi
How to open delete confirmation modal using viewchild
Thanks
Hi
How to open delete confirmation modal using viewchild
Thanks
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jignesh KumarRank #91 · 22.9K reputationPosted May 10, 2024, 1:42 AM EDT
Hello Ramco,
Please use this one,
in your ocmponent,
import { Component, ViewChild, ElementRef } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { @ViewChild('confirmationModal') confirmationModal: ElementRef; constructor() { } confirmDelete(): void { // Display the modal const modal = this.confirmationModal.nativeElement; modal.classList.add('show'); modal.style.display = 'block'; // Handle confirmation logic // You can put your delete logic here or call a method to handle it console.log('Delete confirmed!'); } }Ramco RamcoRank #462 · 5.4K reputationPosted May 10, 2024, 1:56 AM EDT
Hi
I am getting error Object is possibly undefined.
@ViewChild('deleteModal') deleteModal: ElementRef | undefined;
onDelete(Id: number) {
const modal = this.deleteModal.nativeElement;
this.studentList = this.studentList.filter(student => student.id !== Id);
localStorage.setItem("angular17Crud", JSON.stringify(this.studentList));
}
Thanks
Jignesh KumarRank #91 · 22.9K reputationPosted May 10, 2024, 2:17 AM EDT
import { Component, ViewChild, ElementRef, AfterViewInit } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements AfterViewInit { @ViewChild('confirmationModal') confirmationModal!: ElementRef; constructor() { } confirmDelete(): void { // Display the modal const modal = this.confirmationModal.nativeElement; modal.classList.add('show'); modal.style.display = 'block'; // Handle confirmation logic // You can put your delete logic here or call a method to handle it console.log('Delete confirmed!'); } }Amit MohantyRank #112 · 20K reputationPosted May 10, 2024, 2:30 AM EDT
I think the ViewChild decorator doesn't find the element you're trying to query, because deleteModal.nativeElement might be undefined. Make sure you have #deleteModal defined in your HTML where you want to use ViewChild.
Then, in your component class, use ViewChild to reference the element.
import { Component, ViewChild, ElementRef } from '@angular/core'; @Component({ selector: 'app-your-component', templateUrl: './your-component.component.html', styleUrls: ['./your-component.component.css'] }) export class YourComponent { @ViewChild('deleteModal') deleteModal!: ElementRef; onDelete(Id: number) { const modal = this.deleteModal.nativeElement; this.studentList = this.studentList.filter(student => student.id !== Id); localStorage.setItem("angular17Crud", JSON.stringify(this.studentList)); } }