0

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?

1
  • This documentation gives you required information Commented Nov 22, 2017 at 7:32

1 Answer 1

1
@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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.