如何访问Angular中的子包对象
我在app.component.html中使用了一个包如何访问Angular中的子包对象,angular,this,viewchild,Angular,This,Viewchild,我在app.component.html中使用了一个包 <app-my-test2 *ngIf="data" #MyTest2Component></app-my-test2> 在将app-my-test2转换为package之前,所有操作都正常,我访问app-my-test2“this”,但是 将app-my-test2转换为package后,“this”返回未查找的问题是我的*ngIf。*ngIf指令正在终止我的控件组件,因此我无法引用它
<app-my-test2 *ngIf="data" #MyTest2Component></app-my-test2>
在将app-my-test2转换为package之前,所有操作都正常,我访问app-my-test2“this”,但是
将app-my-test2转换为package后,“this”返回未查找的问题是我的*ngIf。*ngIf指令正在终止我的控件组件,因此我无法引用它 前面提到的问题是导致视图未定义的ngIf。答案是使用ViewChildren而不是ViewChildren。我有类似的问题,我不希望网格显示,直到所有的参考数据已经加载
@ViewChildren("MyTest2Component") public myTest2Component: QueryList<MyTest2Component>
public ngAfterViewInit() {
this.myTest2Component.changes.subscribe((comps: QueryList <MyTest2Component>) =>
{
console.log(comps.first) ;
});
}
@ViewChildren(“MyTest2Component”)公共MyTest2Component:QueryList
公共ngAfterViewInit(){
this.myTest2Component.changes.subscribe((comps:QueryList)=>
{
控制台日志(组件优先);
});
}
@ViewChildren("MyTest2Component") public myTest2Component: QueryList<MyTest2Component>
public ngAfterViewInit() {
this.myTest2Component.changes.subscribe((comps: QueryList <MyTest2Component>) =>
{
console.log(comps.first) ;
});
}