Skip to content

Instantly share code, notes, and snippets.

@alincc
Created March 3, 2016 20:01
Show Gist options
  • Save alincc/9c05a2710c5f7d5c2ea6 to your computer and use it in GitHub Desktop.
Save alincc/9c05a2710c5f7d5c2ea6 to your computer and use it in GitHub Desktop.
angular2 - append child to body
import {Component, Input, Output, EventEmitter, ElementRef, ViewChild, DynamicComponentLoader} from 'angular2/core';
import {BrowserDomAdapter} from 'angular2/platform/browser';
@Component({
selector: 'parent',
providers: [BrowserDomAdapter]
})
export class ParentComponent {
@ViewChild(Child) child: Child;
constructor(
el: ElementRef,
dcl: DynamicComponentLoader,
browserDomAdapter: BrowserDomAdapter
) {
}
show() {
this.dcl.loadIntoLocation(Child, this.el, 'dynamicChild')
.then(containerRef => {
this.dialogElement = containerRef.location.nativeElement;
this.browserDomAdapter.appendChild(this.browserDomAdapter.query('body'), this.dialogElement);
});
}
hide() {
this.browserDomAdapter.removeChild(this.browserDomAdapter.query('body'), this.dialogElement);
this.dialogElement = null;
}
}
@touqeershafi
Copy link

touqeershafi commented Apr 13, 2017

Typescript giving me error Angular Core does not have DynamicComponentLoader ?

V: Angular4

@otoomey
Copy link

otoomey commented May 20, 2017

@touqeershafi this doesn't even work for angular2 anymore, because it was written for a very early version of it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment