1
import { Directive, HostListener, HostBinding } from "@angular/core";

I am the beginner in angular. I am making the website in angular through the tutorial of udemy. I want to make the drop-down button. Both codes are correct. but when I click the drop-down button then the dropdown menu didn't open

`@Directive({
selector: '[appdropdown]'
})
export class DropdownDirective  {
@HostBinding('class.Open') isOpen = false; 
@HostListener('click') toggleOpen () {
    this.isOpen = !this.isOpen;
}
}`

The HTML code and the ts code will be correct but it can not function

    <ul class="nav navbar-nav justify-content-end"> 
    <li class=" nav-item  dropdown" appdropdown>
    <a href="#" class=" nav-link dropdown-toggle" role="button">Manage <span 
    class="caret"></span> </a>
    <ul class="dropdown-menu">
    <li>
    <a href="#"> Save Data</a>
    </li>
    <li>
    <a href="#">Fetch Data</a>
    </li>
    </ul>  
    </li>
    </ul>
3
  • Your question is not clear, please explain your question Commented Aug 5, 2018 at 11:53
  • @RashmiKumari i will edit the post kindly check it and give me answer thanks Commented Aug 5, 2018 at 12:35
  • is Open class appended to host on click? Commented Aug 5, 2018 at 13:50

3 Answers 3

1

You need to import your DropdownDirective in your declarations in app.module.

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

Comments

0

Sounds to me like invalid CSS. You are not provided any but I have copy-pasted your example into this Stackblitz

I have added following style

li:not(.Open) .dropdown-menu{
  display: none;
}

obviously it will not do the "dropdown" effect, but demonstrates that overall technique is working

Comments

0
'@Directive({
  selector: '[appToggleDirective]',
})
export class ToggleDirectiveDirective {
  constructor(private _el: ElementRef, private rendered: Renderer2) {}

  @HostBinding('class.show') isOpen = false;

  @HostListener('click') toggleOpen() {
    this.isOpen = !this.isOpen;
    let part = this._el.nativeElement.querySelector('.dropdown-menu');
    if (this.isOpen) {
      this.rendered.addClass(part, 'show');
    } else {
      this.rendered.removeClass(part, 'show');
    }
  }
}'

1 Comment

Your answer could be improved by adding more information on what the code does and how it helps the OP.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.