I have an app with a PrimeNG Angular frontend with menu items.
The menu and its items are already functional using the mouse; my task is to make the menu functional from the keyboard.
Here's a simplified version of what I already have. The two blocks within the ng-container tags are items in the menu.
The goal is that when a menu item is selected - whether by clicking on it with a mouse or tabbing to the item and hitting Enter, it will trigger the menuItemFunction function but not reload the page (ngOnInit will not be called).
<ng-container>
<a
(click)="menuItemFunction('one')">
<span>
one
</span>
</a>
<a
(click)="menuItemFunction('two')">
<span>
two
</span>
</a>
</ng-container>
So this implementation works fine using the mouse to hover over an item and then click on it.
It does not, however, work to use the keyboard to tab to a menu item and hit Enter. The function menuItemFunction never gets called.
Now, if I add a [routerLink] attribute to the items like this, it will call the menuItemFunction function. However, it also triggers the page to reload and thus call ngOnInit - which I do not want.
<ng-container>
<a [routerLink]="'one'"
(click)="menuItemFunction('one')">
<span>
one
</span>
</a>
<a [routerLink]="'two'"
(click)="menuItemFunction('two')">
<span>
two
</span>
</a>
</ng-container>
Is there a way to implement this such that - using either the mouse or the keyboard - it just calls menuItemFunction without reloading the page and also calling ngOnInit()?