Last active
July 16, 2017 05:05
-
-
Save imskojs/3362cb79d8be5011e3b057b850baf54b to your computer and use it in GitHub Desktop.
Angular: get generic parent component
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, Injector, ViewContainerRef } from '@angular/core'; | |
// Injector.view is hidden property. Hence no typing for this. | |
// Below extends abstract injector class. | |
// Note: Class can't be merged so we create interface | |
// which extends Injector class. | |
interface ExtendedInjector extends Injector { | |
view: { component: any } | |
} | |
@Component() | |
class ChildComponent { | |
parentComponent: any | |
constructor( private vcRef: ViewContainerRef ){ | |
this.parentComponent = (this.vcRef.injector as ExtendedInjector).view.component | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment