How to insert a template when interpolating in Angular 4?

I am trying to implement nested translations in Angular (the content of the link has a translation, and this link must be embedded in another translation), and it all comes down to me in the following scenario:

<ng-template #link>
  <a href="#">Test</a>
</ng-template>

<p>Your link: {{link}}</p>

However, in the above case, it linkis a TemplateRef object. So my question is: how to replace the contents of braces with the contents of TemplateRef?

Thank you in advance!

+1
source share
2 answers

Angular 5 update

ngOutletContext was renamed to ngTemplateOutletContext

See also https://github.com/angular/angular/blob/master/CHANGELOG.md#500-beta5-2017-08-29

original

NgTemplateOutlet does what you want:

<p>Your link: <ng-container *ngTemplateOutlet="link"></ng-container></p>

, .

https://angular.io/api/common/NgTemplateOutlet

NgTemplateOutlet StackOverflow .

<ng-template #linkTemplate let-link>
  <a href="#">{{link}}</a>
</ng-template>

<p>Your link: <ng-container *ngTemplateOutlet="linkTemplate; context: {$implicit: link}"></ng-container></p>

+3

. .

    <ng-content *ngIf="!content; else link">

    </ng-content>
    <ng-template #link>
      <div [innerHtml]="otherContent">

      </div>
    </ng-template>
  </div>
0

Source: https://habr.com/ru/post/1685187/


All Articles