Skip to content

Instantly share code, notes, and snippets.

@imskojs
Last active July 16, 2017 05:05
Show Gist options
  • Save imskojs/3362cb79d8be5011e3b057b850baf54b to your computer and use it in GitHub Desktop.
Save imskojs/3362cb79d8be5011e3b057b850baf54b to your computer and use it in GitHub Desktop.
Angular: get generic parent component
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