Reputation: 2748
I am using custom directive and I want to modify classes of a host element upon clicking. Everyone is suggesting to use HostBinding and I tried it but still not working.
@HostBinding('class') nosorting = 'table-sorting';
@HostBinding('class') ascending = 'table-sorting_asc';
@HostBinding('class') descending = 'table-sorting_desc';
@HostListener('click', ['$event'])
onClick(event) {
if (event.target.nodeName === 'TH') {
console.log(this.el);
const target = $(event.target);
if (target.data('sort')) {
if (target.hasClass('table-sorting')) {
this.renderer.addClass(this.el.nativeElement, 'table-sorting_asc');
this.renderer.removeClass(this.el.nativeElement, 'table-sorting');
}
if (target.hasClass('table-sorting_asc')) {
this.renderer.addClass(this.el.nativeElement, 'table-sorting_desc');
this.renderer.removeClass(this.el.nativeElement, 'table-sorting_asc');
}
if (target.hasClass('table-sorting_desc')) {
this.renderer.addClass(this.el.nativeElement, 'table-sorting');
this.renderer.removeClass(this.el.nativeElement, 'table-sorting_desc');
}
}
}
}
Can someone in detail tell me how they work and how to use?
Upvotes: 0
Views: 1848
Reputation: 196
@HostBinding('class.classsName') varName:boolean;
all you to do is set the boolean true or false and then the class will be active in the dom.
Upvotes: 1