In Angular, I would like to use ngClass
and click event to toggle class. I looked through online but some are angular1 and there isn\'t any clear instruction or
Angular6 using the renderer2 without any variables and a clean template:
template:
in ts:
toggleClass(event: any, class: string) {
const hasClass = event.target.classList.contains(class);
if(hasClass) {
this.renderer.removeClass(event.target, class);
} else {
this.renderer.addClass(event.target, class);
}
}
One could put this in a directive too ;)