Created
April 26, 2020 14:03
-
-
Save ShilpaLalwani/0fb65d4131edd5f7a67473fcac767dc8 to your computer and use it in GitHub Desktop.
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, OnDestroy, ViewChild } from '@angular/core'; | |
import { ProfileHostDirective } from './profile-host.directive'; | |
import { ProfileService } from './profile.service'; | |
import { mergeMap, takeUntil } from 'rxjs/operators'; | |
import { Subject } from 'rxjs'; | |
@Component({ | |
selector: 'app-profile-container', | |
template: ` | |
<ng-template appProfileHost></ng-template> | |
` | |
}) | |
export class ProfileComponent implements OnInit, OnDestroy { | |
@ViewChild(ProfileHostDirective, { static: true }) | |
profileHost: ProfileHostDirective; | |
private destroySubject = new Subject(); | |
constructor(private profileService: ProfileService) {} | |
ngOnInit() { | |
const viewContainerRef = this.profileHost.viewContainerRef; | |
this.profileService.isLoggedIn$ | |
.pipe( | |
takeUntil(this.destroySubject), | |
mergeMap(isLoggedIn => | |
this.profileService.loadComponent(viewContainerRef, isLoggedIn) | |
) | |
) | |
.subscribe(); | |
} | |
ngOnDestroy() { | |
this.destroySubject.next(); | |
this.destroySubject.complete(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment