Building and Using a Dropdown Directive
1. In shared folder create a dropdown directive
It could be any name.
Our goal is to open or manipulate our dropdown button using angular not bootsrap
ng g directive dropdown
2. We need to add a certain css class specifically Open class.
we want to make it clickable to toggle off our dropdown button.
-We added @HostListener
-and pass in our click event
-then we added a method called toggleOpen(){} but it could be any name.
-we set isOpen property and set to false;
-in toggleOpen(){} we access our isOpen property and assign it to below value
-we need to attached the css class called open dynamically with @HostBinding on
our property'
must be imported as well.
-our css class open will only be attached dynamically on our element if ti will
become true.
and it will become true once we have click our our dropdown button.
when switches to false it will be removed.
********
import { HostBinding } from '@angular/core';
import { Directive, HostListener } from '@angular/core';
@Directive({
selector: '[appDropdown]'
})
export class DropdownDirective {
@HostBinding('[Link]') isOpen = false;
constructor() { }
@HostListener("click") toggleOpen() {
[Link] = ![Link];
}
3. in [Link] we need to add our Dropdown Directive
since we did it by cli it automatically being added.
4. Now we'lll go got [Link] where we will put our appDropdown
directiveclass on our Dropdown button.
<div class="btn-group" appDropdown>
5. in [Link]
<li class="dropdown" appDropdown>