我正在努力完成的总结
ViewContainerRef(完成)问题
ViewContainerRefViewContainerRef提供方法,例如get(index)return ViewRef。ViewRef似乎与Component实例没有任何关系,这使得获取所需数据成为一项挑战这是一个 Stackblitz 链接,其工作代码如下所示(用于动态创建组件)
在appComponent通过创建使用一些部件开始了ComponentFactoryResolver,并且将它们添加到ViewChild在模板中所定义。每个DynamicComponent都使用id我们在创建后尝试引用的属性值进行初始化
@Component({
selector: "my-app",
template: `
<h3>Retrieving Component Reference for Dyamic Compnents</h3>
<button (click)="getCompRef()">Get References</button>
<div>
<ng-container #childCont></ng-container>
</div>
<div>
<small>List out the Ids from the dynamic components</small> <br />
{{ createdItemIds | json }}
</div>
`,
styleUrls: ["./app.component.css"]
})
export class AppComponent implements AfterViewInit …Run Code Online (Sandbox Code Playgroud)