Created
April 5, 2019 18:15
-
-
Save hoffination/de5778453ca1d3e635f830e8c8f5d925 to your computer and use it in GitHub Desktop.
Angular click outside component functionality
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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