Ngx-bootstrap: [BS-dropdown]How to solve "click outside dropdown cannot work ngx-bootstrap version ^1.9.2" problem temporarily?

Created on 13 Sep 2017  路  7Comments  路  Source: valor-software/ngx-bootstrap

Hi everyone,
I updated angular to 4.3.6 and ngx-bootstrap to lasted version(^1.9.2).

<div class="btn-group" dropdown [autoClose]="outsideClick">
      <button class="indicator-button" dropdownToggle type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
      </button>
      <ul class="dropdown-menu" *dropdownMenu aria-labelledby="dropdownMenu1">
        <li *ngFor="let note of notes">
          <div class="row">abc
          </div>
        </li>
      </ul>
    </div>

But outsideClick cannot work when I clicked outside of dropdown. I read ngx-bootstrap documentation and I find autoClose only support true or false to close dropdown.
I have a solution by use directive to check user click inside or outside dropdown:

@Directive({
  selector: '[dropdownOutsideClick]'
})
export class DropdownOutsideClickDirective {

  constructor(private _elementRef: ElementRef) {
  }

  @Output()
  public onDropdownOutsideClick = new EventEmitter<any>();

  @HostListener('document:click', ['$event.target'])

  public onClick(targetElement) {
    const clickedInside = this._elementRef.nativeElement.contains(targetElement);
    if (!clickedInside) {
      this.onDropdownOutsideClick.emit({clickOutside: true});
    }
  }
}

and
<div [autoClose]="false" dropdown dropdownOutsideClick (onDropdownOutsideClick)="onDropdownOutsideClick($event)" [(isOpen)]="isDropdownOpen">
and set isDropdownOpen = false if has emit from onDropdownOutsideClick to close dropdown if user click outside.

But I think It's not good code and solution.

How to solve "click outside dropdown cannot work ngx-bootstrap version ^1.9.2" problem temporarily?
Thanks.

comp(dropdown)

Most helpful comment

A workaround i found is this:

  • Set dropdown autoClose to true
    <div dropdown [autoClose]="true">
  • Bind a click listener to the parent dropdown-menu
    <div *dropdownMenu class="dropdown-menu" role="combobox" (click)="preventClose($event)">
  • And then in your component, the function your calling from the click listener
    preventClose(event: MouseEvent) {
    event.stopImmediatePropagation();
    }

Hope it works for you @benj0c

All 7 comments

Why don't you use [autoClose]="true"?

Hi @IlyaSurmay,
I want to do actions on elements of dropdown that will not close. It only close when I clicked outside of dropdown.
Thanks.

@tamvo94 Did you find workarounds to achieve this ?

@Anirudh-Konduri the workaround is edited in the first post. Confirmed working by me 馃憤

ngx-bootstrap: 1.9.3
angular: 4.4.6

Merged and released in v2+

@valorkin what is merged and released in v2+? How can I now achieve that only outside click close a dropdown?

A workaround i found is this:

  • Set dropdown autoClose to true
    <div dropdown [autoClose]="true">
  • Bind a click listener to the parent dropdown-menu
    <div *dropdownMenu class="dropdown-menu" role="combobox" (click)="preventClose($event)">
  • And then in your component, the function your calling from the click listener
    preventClose(event: MouseEvent) {
    event.stopImmediatePropagation();
    }

Hope it works for you @benj0c

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hugonne picture hugonne  路  3Comments

tpiros picture tpiros  路  3Comments

RolfVeinoeSorensen picture RolfVeinoeSorensen  路  3Comments

KimBum picture KimBum  路  3Comments

Scooviese picture Scooviese  路  3Comments