如何在组件代码中声明TemplateRef的实例,以便在标记中多次重用它?

问题描述 投票:0回答:1
angular ng-template
1个回答
0
投票

你不应该用任何东西代替???。

@ContentChild
装饰器将确保 Angular 使用相应的内容子项填充类中的字段。

在某些项目中,您可以满足用于明确且干净的模板标记的辅助指令。在你的情况下,它看起来像这样:

@Directive({selector: '[lookupContent]'})
class LookupContentDirective{
  constructor(public template: TemplateRef)
}

....
class AppLookupComponent {
  @ContentChild(LookupContentDirective) content: LookupContentDirective;
...
// app-lookup.component.html
<div *ngTemplateOutlet="content.template; context: context"></div>
...
// usage
<app-lookup>
 <li *lookupContent>li will be a template</li>
</app-lookup>
© www.soinside.com 2019 - 2024. All rights reserved.