I'm trying to simulate a click on a button in Angular. But it's not working. I imported ViewChild
and ElementRef
. Then added the hash reference to the button. and then called the click but it's not working. this is the HTML:
<button #filterPanelButton id="loadFiltersButton" class="details-filters-button" mat-button [ngClass]="{'filter-button-active': loadButtonChanged}"
type="button"[matMenuTriggerFor]="loadDetailsFilter" (menuOpened)="detailsFiltersOpened()">
<span id="loatFilterButtonText" class="details-filters-button-text">{{loadButtonText}}</span>
<mat-icon class="menu-active" [ngClass]="{'menu-open': arrowUp}">arrow_drop_down</mat-icon>
</button>
and this is the .ts method:
lauchSearchAndCLoseFiltersPanel($event){
this.detailsFiltersClosed($event);
// document.getElementById('loadFiltersButton').click();
this.filterPanelButton.nativeElement.click();
}
If I uncomment the document.getElementById('loadFiltersButton')
it works, but I've heard that it is discouraged to use that method in Angular.
Why might the viewChild method not be working?