Решением для простого случая введения HTML содержимого является использование <ng-content>
.
parent
<child>
<p>Some injected HTML content</p>
</child>
child
<p>
<!-- inner HTML of <child> will go here -->
<ng-content></ng-content>
</p>
ngTemplateOutlet
Если вы хотите динамически внедрить HTML, вы просто динамически объявляете HTML внутри <child>
:
parent
<child>
<p *ngIf="condition1">Some conditional injected HTML content</p>
<p *ngIf="condition2">Some different conditional injected HTML content</p>
</child>
Вы можете использовать ngTemplateOutlet
, если хотите сослаться на именованный шаблон:
parent
<child>
<ng-container *ngTemplateOutlet="body"></ng-container>
</child>
<ng-template #body>
<p>Some reusable content</p>
</ng-template>
Введенный таргетинг HTML
Чтобы нацелиться на определенные c части введенного HTML, используйте селекторы:
parent
<child>
<span heading>TITLE</span>
<p body>The body</p>
</child>
ребенок
<h1><ng-content select="[heading]"></ng-content></h1>
<div select="[body]">
</div>