I'm working with angular-cli and I have a menu structured as follow(ul->li guided):
<ul class="sub-menu">
<li class="nav-item ">
<a routerLink="/logistica/contenedores" class="nav-link ">
<span class="title">Adicionar</span>
</a>
</li>
</ul>
the problem is when I'm using routerlink. I have a javascript that manage the events on menu but my question is If I could override that javascript with a directive or something else.
The routerlink works perfectly for tags outside the menu structure. Also I implemented a directive but is not catching the event not even pass for console.log:
import {AfterViewInit, Directive, ElementRef, Input, HostBinding, HostListener} from '@angular/core';
@Directive({
selector: '[appHrefPreventDefault]',
// host: {'(click)': 'preventDefault($event)'},
})
export class HrefPreventDefaultDirective {
@Input() href: string;
@HostBinding('onclick') preventDefault2(event) {
if (this.href.length === 0 || this.href === '#') {
event.preventDefault();
}
console.log('href directive');
}
@HostListener('onclick') preventDefault(event) {
if (this.href.length === 0 || this.href === '#') {
event.preventDefault();
}
console.log('href directive');
}
constructor(private el: ElementRef) {
}
}
Thanks in advance!