问题描述
我正在尝试动态创建和显示组件,我需要向其中传递一些数据,以便它知道要显示什么.
I'm trying to dynamically create and show component and i need to pass some data into it, so that it knows what to show.
这是我的代码:
html部分:
<div class="basicContainer">
<div class="projectsTreeContainer">
<input type="text" id="searchWord" placeholder="Search through projects"/>
<button (click)="loadAddProject()">Add new Project</button>
<app-projects-tree (onLoadProjectDetails)="loadProjectDetails($event)"
(onLoadWpDetails)="loadWpDetails($event)"
(onSelectAndLoadJobDetails)="loadJobDetails($event)"></app-projects-tree>
</div>
<div class="infoContainer">
<ng-container *ngComponentOutlet="details"></ng-container>
</div>
</div>
组件:
export class ProjectsComponent implements OnInit {
details: Component;
private showWp: boolean;
constructor() {
}
loadProjectDetails(project: BasicProject): void {
this.details = new ProjectComponent(project);
}
以及我要动态创建和显示的组件:
and the component i want dynamically create and show:
export class ProjectComponent implements OnInit {
project: Project;
constructor(basicProject: BasicProject) {
this.project = new Project();
this.project.name = basicProject.name ;
}
NgModule:
@NgModule({
declarations: [
AppComponent,
NavbarComponent,
LoginComponent,
ProjectsComponent,
ProjectComponent,
ProjectsTreeComponent,
TreeProjectComponent,
TreeWpComponent,
WpComponent,
JobComponent,
AddProjectComponent
],
imports: [
BrowserModule,
FormsModule,
RouterModule.forRoot(
AppRoutes.appRoutes,
// {enableTracing: true} // <-- debugging purposes only
),
HttpClientModule,
HttpModule
],
providers: [
AuthenticationService,
ProjectsService,
CanActivateAuthGuard,
{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
multi: true
}],
bootstrap: [AppComponent],
entryComponents: [ProjectComponent, WpComponent, JobComponent, AddProjectComponent]
})
错误消息:
我该如何完成任务?谢谢.
How can i do the task?Thanks.
推荐答案
如果打开 NgComponentOutlet
指令的代码:
@Directive({selector: '[ngComponentOutlet]'})
export class NgComponentOutlet implements OnChanges, OnDestroy {
@Input() ngComponentOutlet: Type<any>;
@Input() ngComponentOutletInjector: Injector;
您会注意到,它需要输入 ngComponentOutlet
输入,该输入应具有 Type< any>
类型,但是您正在将对象传递给它.
you can notice that it takes ngComponentOutlet
input that should have type Type<any>
but you're passing object to it.
我们还可以看到该指令可以将 Injector
用作 @Input
.因此,让我们利用这些知识来完成您的任务.
Also we can see that this directive can take Injector
as @Input
. So lets leverage this knowledge to do your task.
例如,我们编写了如下模板:
For example we wrote template like:
<ng-container *ngComponentOutlet="component; injector: injector"></ng-container>
现在让我们在组件类中声明 component
和 injector
属性:
Now lets declare component
and injector
properties in component class:
@Component({...})
export class AppComponent {
component: Type<any>;
injector: Injector;
constructor(private inj: Injector) {}
ngOnInit() {
this.component = ProjectComponent; // note: we're passing type here
this.injector = Injector.create([
{ provide: BasicProject, useValue: new Project('test name') }
], this.inj);
}
}
和您的 ProjectComponent
现在使用角度DI机制来获取传递给注射器的数据:
and your ProjectComponent
now uses angular DI mechanism to get data we passed to injector:
export class ProjectComponent {
name: string;
constructor(project: BasicProject) {
this.name = project.name;
}
}
Stackblitz Example
这篇关于Angular:如何通过Injector传递ngComponentOutlet中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!