15.0.1 • Published 1 month ago

ng-click-outside2 v15.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

ng-click-outside2

NPM License Love Angular badge GitHub issues GitHub stars

Angular directive for handling click events outside an element.

Like binding to a regular click event in a template, you can do something like this:

<div (clickOutside)="onClickedOutside($event)">My element</div>

Table of contents

  1. Installation
  2. Compatibility
  3. Options
  4. Usage
  5. Migration from ng-click-outside

Installation

npm install --save ng-click-outside2

Compatibility

AngularVersionNPM
1310.x.xng-click-outside2@^10.0.0
14,15,1611.x.xng-click-outside2@^11.0.0
16,1712.x.xng-click-outside2@^12.0.0
1713.x.xng-click-outside2@^13.0.0
1714.x.xng-click-outside2@^14.0.0
1715.x.xng-click-outside2@^15.0.0

If you use Angular <= 12 please use the original package. https://www.npmjs.com/package/ng-click-outside

Options

Property nameTypeDefaultDescription
attachOutsideOnClickbooleanfalseBy default, the outside click event handler is automatically attached. Explicitely setting this to true sets the handler after the element is clicked. The outside click event handler will then be removed after a click outside has occurred. (Import: NgClickOutsideAttachOutsideDirective)
clickOutsideEnabledbooleantrueEnables directive.
clickOutsideEventsstring'click'A comma-separated list of events to cause the trigger. For example, for additional mobile support: [clickOutsideEvents]="'click,touchstart'".
delayClickOutsideInitbooleanfalseDelays the initialization of the click outside handler. This may help for items that are conditionally shown (see issue #13). (Import NgClickOutsideDelayOutsideDirective)
clickOutsideEmitOnBlur--If enabled, emits an blurWindow event when user clicks outside of applications' window while it's visible. Especially useful if page contains iframes. (Import NgClickOutsideEmitOnBlurDirective)
clickOutsideExcludestringA comma-separated string of DOM element queries to exclude when clicking outside of the element. (Import NgClickOutsideExcludeDirective) For example: [clickOutsideExclude]="'button,.btn-primary'".

Example Usage

Add NgClickOutsideDirective to your imports:

import {NgClickOutsideDirective} from 'ng-click-outside2';

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, NgClickOutsideDirective],
  bootstrap: [AppComponent]
})
class AppModule {}

You can then use the directive in your templates:

@Component({
  selector: 'app',
  template: `
    <div (clickOutside)="onClickedOutside($event)">Click outside this</div>
  `
})
export class AppComponent {
  onClickedOutside(e: Event) {
    console.log('Clicked outside:', e);
  }
}

Usage with Excluding classes

import {NgClickOutsideDirective} from 'ng-click-outside2';

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, NgClickOutsideDirective, NgClickOutsideExcludeDirective],
  bootstrap: [AppComponent]
})
class AppModule {}

You can then use the directive in your templates:

@Component({
  selector: 'app',
  template: `
    <div (clickOutside)="onClickedOutside($event)" [clickOutsideExclude]="'.foo'">Click outside this</div>
  `
})
export class AppComponent {
  onClickedOutside(e: Event) {
    console.log('Clicked outside:', e);
  }
}

Migration from ng-click-outside

// change imports
import { ClickOutsideModule } from 'ng-click-outside';
// to
import {NgClickOutsideDirective} from 'ng-click-outside2';

// and in Module import
ClickOutsideModule
// to
NgClickOutsideDirective

15.0.1

1 month ago

15.0.0

2 months ago

14.0.0

2 months ago

14.0.1

2 months ago

13.0.0

6 months ago

12.0.0

9 months ago

11.2.0

12 months ago

11.0.0-next-0

2 years ago

11.0.0

2 years ago

11.1.0

2 years ago

10.0.4

2 years ago

10.0.3

2 years ago

10.0.2

2 years ago

10.0.1

2 years ago

10.0.0

2 years ago