如何使用来自其他 Angular 组件的`templateref`? [英] How to use `templateref` from other Angular component?
问题描述
如何使用其他组件模板文件中的templateRef
?
How to use templateRef
from other component template file?
我有 BatmanComponent
、SpidermanComponent
和 JokerComponent
.其中一些具有相似的功能,因此我决定创建一个 HumanComponent
,并将所有可重用的 HTML 代码放在该文件中,如下所示:
I have BatmanComponent
, SpidermanComponent
and a JokerComponent
. 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
永远不会被使用,它只是一个包含所有默认模板的文件.
Note: the HumanComponent
will never be used, it is just a file that consists all default template.
<!-- 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>
我可以知道如何注入这些模板(来自 human.component.ts)并在 batman.component.ts
中使用它,等等...?
May I know how to inject those template (from human.component.ts) and use it inside the batman.component.ts
, etc...?
我知道我可以这样做:(只有如果模板代码被复制粘贴到蝙蝠侠、蜘蛛侠和小丑 HTML文件中)>
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)
<!-- 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 -->
我可以知道如何将导出 templateRef
到其他文件并重新使用它们?我不想在这些文件中复制和粘贴类似的代码,我希望我可以有一个默认的模板文件,然后将这些代码导出给任何想要它的人.可能吗?
May I know how can I export templateRef
to other files, and re-use them? 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?
更新:
阅读评论后,我决定使用可重用组件,而不是这种技术"……可能,Angular 团队正在努力优化可重用组件方法(如@artyom,@rafael,@ibenjelloun 建议),那么可能只是按照他们的方式走会更明智......哈哈......
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...
无论如何,谢谢.
推荐答案
如果您创建一个 TemplatesService
,您可以在其中注册您的模板并在其他组件中使用它们:
If you create a TemplatesService
, you can register your templates in it and use them in other components :
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>
这篇关于如何使用来自其他 Angular 组件的`templateref`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!