0

Here's my code:

@Directive({
  selector: '[appDropdown]',
})

export class DropdownDirective {
  isClassApplied: boolean;
  @Input('appDropdown') className: string;

  constructor(private renderer: Renderer2, private elRef: ElementRef) {
    this.isClassApplied = false;
  }

    @HostListener('click') onClickAddClass() {
        if (!this.isClassApplied) {
            this.isClassApplied = true;
            this.renderer.addClass(this.elRef, this.className);
        } else {
            this.isClassApplied = false;
            this.renderer.removeClass(this.elRef, this.className);
        }
    }
}

Here's the html: CodePen Link

It is giving me an error when trying to add a class on the elRef. Can anyone please tell me what's wrong in this.

1
  • What is the error you get ? Commented Feb 11, 2023 at 11:07

3 Answers 3

1

What happens here is i haven't given the correct argument to addClass function. I must have to pass the element on which the directive sits on. And that should be found under the nativeElement of the ElementRef.

So the code look like this:

@Directive({
  selector: '[appDropdown]',
})

export class DropdownDirective {
  isClassApplied: boolean;
  @Input('appDropdown') className: string;

  constructor(private renderer: Renderer2, private elRef: ElementRef) {
    this.isClassApplied = false;
  }

    @HostListener('click') onClickAddClass() {
        if (!this.isClassApplied) {
            this.isClassApplied = true;
            this.renderer.addClass(this.elRef.nativeElement, this.className);
        } else {
            this.isClassApplied = false;
            this.renderer.removeClass(this.elRef.nativeElement, this.className);
        }
    }
}

Now, it should work as expected.

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

Comments

0

I'm not sure what the error is exactly, but notice that the input in your directives should be without the bindingPropertyName, like that:

@Input() appDropdown: string;

Then in your component you can write:

<div [appDropdown]="class_name"></div>

source

2 Comments

Error is exactly what's in the title, but this solution doesn't work error persists anyways. Can you suggest any other solution? Thanks.
Here's is the image of an error: ibb.co/x8Ntb93
0

It seems the DOM element is missing when the directive tries to add a class name. This issue may arise when the directive appDropDown is inside *ngIf structural directive. Trying using [hidden] property or use css opacity property in case you are using ngIf.

2 Comments

Here's the html of it: codepen.io/shubhRepository/pen/mdjNjZZ. Btw i don't have any structural directives.
The codepen which you share doesn't seem to replicate the issue, could you share it in stackblitz?

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.