Angular 2: A Simple Click Outside Directive

Detecting clicks on a component is easy in Angular 2: Just use the (click) event binding, pass a function from the component’s instance and you are done. However, if you need to detect clicks outside of your component, things are getting tricky. You will usually need this for custom implementations of drop-down lists, context menus, pop-ups or widgets.

As this is a functionality which you might use more often, you should wrap it in a reusable directive. Angular 2 offers a syntactically nice way to implement such a directive. So let’s go ahead and implement a simple click-outside directive in Angular 2!

tl;dr: Implementing this directive is super easy. If you feel lazy, just run npm i angular2-click-outside.

Continue reading Angular 2: A Simple Click Outside Directive