Skip to content

Instantly share code, notes, and snippets.

@hoffination
Created April 5, 2019 18:15
Show Gist options
  • Save hoffination/de5778453ca1d3e635f830e8c8f5d925 to your computer and use it in GitHub Desktop.
Save hoffination/de5778453ca1d3e635f830e8c8f5d925 to your computer and use it in GitHub Desktop.
Angular click outside component functionality
import { Component, OnInit, ElementRef } from '@angular/core';
import { PostsService } from '../post-service';
import 'rxjs/add/operator/map'
@Component({
selector: 'app-post-listing',
templateUrl: './post-listing.component.html',
styleUrls: ['./post-listing.component.css'],
host: {
'(document:click)': 'onClick($event)',
},
})
export class PostListingComponent implements OnInit {
postDataListing: any = [];
constructor(
private _eref: ElementRef,
private PostsServiceList: PostsService
) {}
ngOnInit() {
this.PostsServiceList.getPostData().subscribe(data => this.postDataListing = data);
}
onClick(event) {
if (!this._eref.nativeElement.contains(event.target)) {
console.log('click outside');
} else {
console.log('click inside');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment