简体   繁体   English

如何使用来自其他 Angular 组件的`templateref`?

[英]How to use `templateref` from other Angular component?

How to use templateRef from other component template file?如何使用templateRef来自其他组件的模板文件?

I have BatmanComponent , SpidermanComponent and a JokerComponent .我有BatmanComponentSpidermanComponentJokerComponent Some of them have similar features, so that I decided to create a HumanComponent , and put all reusable HTML codes in that file, as such:其中一些具有类似的功能,因此我决定创建一个HumanComponent ,并将所有可重用的 HTML 代码放在该文件中,如下所示:

Note: the HumanComponent will never be used, it is just a file that consists all default template.注意: HumanComponent永远不会被使用,它只是一个包含所有默认模板的文件。

<!-- human.component.ts -->
<ng-template #eye>
    Eyes: {{ size }}
</ng-template>
<ng-template #body>
    Body: bust {{ x[0] }}, waist {{ x[1] }}, hip {{ x[2] }} 
</ng-template>

May I know how to inject those template (from human.component.ts ) and use it inside the batman.component.ts , etc...?我可以知道如何注入这些模板(来自human.component.ts )并在batman.component.ts等中使用它......?

I know I can do it as such: (only if the template code is being copy-paste inside the batman, spiderman, and joker HTML files)我知道我可以这样做:(仅模板代码被复制粘贴到蝙蝠侠、蜘蛛侠和小丑HTML 文件中时)

<!-- batman.component.ts -->
<ng-container *ngTemplateOutlet="eye; context: contextObj"></ng-container>

<ng-template #eye> ... </ng-template> <!-- need to copy/paste here, and use it locally -->

May I know how can I export templateRef to other files, and re-use them?我可以知道如何templateRef导出到其他文件并重新使用它们吗? I don't want to copy and paste similar codes across those files, I hope I can have a default template file, and just export those codes to whoever want it.我不想在这些文件中复制和粘贴类似的代码,我希望我可以有一个默认的模板文件,然后将这些代码导出给任何想要它的人。 Is it possible?是否可以?


Update:更新:

After reading the comments, I decided to use the reusable component rather than this "techniques"... Probably, the Angular team is trying hard to optimize the reusable component approach (as @artyom, @rafael, @ibenjelloun suggested), then probably just follow their path will be wiser... Haha...阅读评论后,我决定使用可重用组件而不是这种“技术”......可能,Angular 团队正在努力优化可重用组件方法(如@artyom、@rafael、@ibenjelloun 建议的那样),然后可能跟着他们的路走会更聪明……哈哈……

Anyway, thanks.不管怎样,谢谢。

If you create a TemplatesService , you can register your templates in it and use them in other components :如果您创建TemplatesService ,您可以在其中注册您的模板并在其他组件中使用它们:

import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class TemplatesService {
  templates = {};
  add(name: string, ref) {
    this.templates[name] = ref; 
  }
  get(name: string) {
    return this.templates[name];
  }
}

You can then add your templates to the service from a root component :然后,您可以从根组件将模板添加到服务中:

{{ _templatesService.add('template1', template1) }}
{{ _templatesService.add('template2', template2) }}

<ng-template #template1 let-name="name">
  ****** Template 1 {{name}} ******
</ng-template>

<ng-template #template2 let-name="name">
  ----- Template 2 {{name}} ------
</ng-template>

And use them in another component :并在另一个组件中使用它们:

<ng-container *ngTemplateOutlet="_templatesService.get('template1'); 
 context: {name: 'From Hello Component'}"></ng-container>

Here is a stackblitz demo.这是一个 stackblitz 演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM