Working on a project with Angular 11, Angular material, and Bootstrap, I encountered an issue. I want to display a popup ad the first time a user visits the home page. The modal dialog is created using Angular material, and I have it in the ads component, which is then called in the home component's ngOnInit
function to show the dialog when the page loads. Despite trying some solutions involving JS, none of them worked for me. Any guidance on how I can resolve this?
In my ads component HTML, only the image is displayed without a close button. However, I am open to adding one for a solution if necessary.
<mat-dialog-content id="myModal" class="gradient-border">
<img style="max-width: 100%" src="../../../assets/img/modal-ad.jpg" />
</mat-dialog-content>
ads component ts
import { Component, OnInit } from '@angular/core';
import { MatDialogRef } from '@angular/material/dialog';
@Component({
selector: 'app-anuncios',
templateUrl: './anuncios.component.html',
styleUrls: ['./anuncios.component.css'],
})
export class AnunciosComponent implements OnInit {
constructor(public dialogRef: MatDialogRef<AnunciosComponent>) {}
ngOnInit(): void {}
}
Home component ts
import { Component, OnInit } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { AnunciosComponent } from '../anuncios/anuncios.component';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css'],
})
export class HomeComponent implements OnInit {
constructor(public dialog: MatDialog) {}
ngOnInit(): void {
this.showDialog();
}
showDialog() {
const dialogRef = this.dialog.open(AnunciosComponent, {
maxWidth: '100vw',
maxHeight: '150vw',
panelClass: ['animate__animated', 'animate__bounceInDown'],
});
}
}
The current implementation always displays the modal dialog when the home page loads, but I specifically need it to show only the first time the user visits the page. I explored options involving cookies or JS functions, but couldn't get them to work effectively for my project. Being new to this, I may not have utilized those solutions correctly. Any suggestions would be greatly appreciated.