angular 怎么样在组件模板中输出子组件(以获取到子组件对象)

问题描述
我现在需要控制子组件的显示方式,目前已经获取到子组件component的对象实例,但是我怎么把这个对象显示在父组件的模板中呢?
问题出现的环境背景及自己尝试过哪些方法
这样不得行
相关代码
import{Component,OnInit,ViewContainerRef,TemplateRef,ContentChildren,AfterViewInit}from'@angular/core';
import{ISelectItem}from'./item-interface';
@Component({
selector:'zzj-table-tool-select',
templateUrl:'./table-tool-select.component.html',
styleUrls:['./table-tool-select.component.scss']
})
exportclassTableToolSelectComponentimplementsOnInit,AfterViewInit{
children:ISelectItem[]=[];
@ContentChildren('child')child;
constructor(){}
ngOnInit(){
}
publicaddItem(item:ISelectItem){
this.children.push(item);
}
ngAfterViewInit(){
console.log('--------');
console.log(this.child)
}
}
GCT1015
浏览 1288回答 2
2回答

慕神8447489

语法糖:直接引用子组件类,不用类实例。看下官网

慕斯709654

ng-content不对啊,你没输出怎么获取的实例?还是说你只写了@ContentChildren('child')child;但模板中没有与之对应的代码?这样是获取不到实例的
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript