不知道爲什麼你需要它,但這裏是一些建議:
import {Component, NgModule, Input, VERSION, ChangeDetectorRef} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import {Injectable, ComponentFactoryResolver, ApplicationRef, Injector} from '@angular/core';
@Component({
selector:'my-cmp',
template: 'here is {{title}}'
})
export class MyComponent {
@Input() title: string;
}
@Component({
selector: 'my-app',
template: `
<input #inp>
<button (click)="create(inp.value)">Create</button>
`,
})
export class App {
constructor(private componentFactoryResolver: ComponentFactoryResolver,
private appRef: ApplicationRef,
private injector: Injector) {
}
create(title) {
let factory = this.componentFactoryResolver.resolveComponentFactory(MyComponent);
let ref = factory.create(this.injector);
ref.instance.title = title;
this.appRef.attachView(ref.hostView);
console.log(ref.location.nativeElement);
this.appRef.detachView(ref.hostView);
}
}
@NgModule({
imports: [ BrowserModule ],
declarations: [ App , MyComponent],
entryComponents: [MyComponent],
bootstrap: [ App ]
})
export class AppModule {}