Angular @ContentChildren和ng模板有问题
我有一个例子,我试图使用包装器指令中的Angular @ContentChildren和ng模板有问题,angular,typescript,Angular,Typescript,我有一个例子,我试图使用包装器指令中的ContentChildren获取所有指令 具体来说,我在包装器元素上设置了LookupContainerDirective。然后,在包装器元素中,我有一些模板,模板本身有LookupdDirective。我正在尝试获取LookupContainerDirective中的所有LookupdDirective引用 app.component.html <div appLookupContainer> <!-- <div appLoo
ContentChildren
获取所有指令
具体来说,我在包装器元素上设置了LookupContainerDirective
。然后,在包装器元素中,我有一些模板,模板本身有LookupdDirective
。我正在尝试获取LookupContainerDirective
中的所有LookupdDirective
引用
app.component.html
<div appLookupContainer>
<!-- <div appLookupd></div> -->
<ng-container *ngTemplateOutlet="myTemp"></ng-container>
</div>
<ng-template #myTemp>
<div appLookupd></div>
</ng-template>
查找容器.directive.ts
@ContentChildren(LookupdDirective, { descendants: true })
private _lookupDirectives: QueryList<LookupdDirective>;
ngAfterContentInit() {
console.log(this._lookupDirectives);
}
@ContentChildren(LookupdDirective,{后代:true})
private\u lookupdirections:QueryList;
ngAfterContentInit(){
console.log(this.\u lookupdirections);
}
我还准备了示例(检查控制台输出)
我希望控制台输出对LookupdDirective
的所有引用,但它显示没有引用(\u results:Array[0]
)。如果我不使用模板,而是使用常规元素,则会找到引用(\u results:Array[1]
)。对此有一个解释
一个建议性的解决办法是:
如前所述
在角度内容投影和查询中,它表示“内容原样”
在模板中编写“而不是在DOM中可见的内容”
似乎在期待
看看这里,很相似,我想谢谢,这解决了我的问题。。。尽管如此,我还是希望他们能以一种能让我的例子起作用的方式来实现它。。。