Skip to content

Commit

Permalink
fix(helpers): add a way to set root view component ref
Browse files Browse the repository at this point in the history
fixes #1056
  • Loading branch information
valorkin committed Oct 10, 2016
1 parent b80c0b4 commit 79d3335
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions components/utils/components-helper.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { DOCUMENT } from '@angular/platform-browser';
*/
@Injectable()
export class ComponentsHelper {
public root:ViewContainerRef;

public constructor(private applicationRef:ApplicationRef,
private componentFactoryResolver:ComponentFactoryResolver,
private injector:Injector) {
Expand All @@ -21,25 +23,36 @@ export class ComponentsHelper {
}

/**
* This is a name conventional class to get application root view component ref
* In some cases, like using ngUpgrate,
* you need to explicitly set view container ref
* to made this method working you need to add:
* ```typescript
* @Component({
* selector: 'my-app',
* ...
* })
* export class MyApp {
* constructor(viewContainerRef: ViewContainerRef) {
* constructor(componentsHelper:ComponentsHelper, viewContainerRef: ViewContainerRef) {
* // A Default view container ref, usually the app root container ref.
* // Has to be set manually until we can find a way to get it automatically.
* this.viewContainerRef = viewContainerRef;
* componentsHelper.setRootViewContainerRef(viewContainerRef)
* }
* }
* ```
*/
public setRootViewContainerRef(value:ViewContainerRef):void {
this.root = value;
}
/**
* This is a name conventional class to get application root view component ref
* @returns {ViewContainerRef} - application root view component ref
*/
public getRootViewContainerRef():ViewContainerRef {
// https://github.com/angular/angular/issues/9293
if (this.root) {
return this.root;
}

const comps = this.applicationRef.components;

if(!comps.length) {
Expand All @@ -48,8 +61,9 @@ export class ComponentsHelper {

try {
/* one more ugly hack, read issue above for details */
const root = (this.applicationRef as any )._rootComponents[0];
return root._hostElement.vcRef;
const rootComponent = (this.applicationRef as any )._rootComponents[0];
this.root = rootComponent._hostElement.vcRef;
return this.root;
} catch (e) {
throw new Error(`ApplicationRef instance not found`);
}
Expand Down

0 comments on commit 79d3335

Please sign in to comment.