bootstrap 4 in Angular 2 dropdown not working

后端 未结 14 1354
旧巷少年郎
旧巷少年郎 2021-02-04 08:36

I have followed the following to install bootstrap 4 into my Angular 2 project: Accepted Answer, following the first 1,2,3 and 4 steps

However when I add the following <

14条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-04 09:26

    Variation on @VictorLuchian for newest BS4 beta, it seems 'show' class need to be added on the dropdown-menu as well. This version include a click outside close instead of mouseout

    import { Directive,HostListener,HostBinding, ElementRef } from '@angular/core'; 
    @Directive({
      selector: '[customdropdown]'
    })
    export class CustomDropdownDirective {
    
    private isOpen: boolean =false;
    constructor(private _el: ElementRef) { 
    
    }
    
    @HostBinding('class.show') get opened() {
        return this.isOpen;
    }
    @HostListener('click') open() {
        this.isOpen = true;
        this._el.nativeElement.querySelector('.dropdown-menu').classList.add('show')                
    }
    @HostListener('document:click', ['$event.target']) close (targetElement) {
        let inside: boolean = this._el.nativeElement.contains(targetElement);
        if(!inside) {
            this.isOpen = false;
            this._el.nativeElement.querySelector('.dropdown-menu').classList.remove('show')
        }
    }
    }
    

提交回复
热议问题