Skip to content

Instantly share code, notes, and snippets.

@guillaumegarcia13
Created September 4, 2017 15:53
Show Gist options
  • Star 35 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save guillaumegarcia13/a00379681d95b0176e10fd3f794bd8bb to your computer and use it in GitHub Desktop.
Save guillaumegarcia13/a00379681d95b0176e10fd3f794bd8bb to your computer and use it in GitHub Desktop.
Angular 4 ng-template & ng-container with parameters
<ng-template #followingpost let-author="author" let-age="age" let-text="text" let-badge="badge">
<div class="container-fluid">
<div class="card">
<div class="header">
<h4 class="title">{{ author }}</h4>
<p class="category">il y a {{ age }} jours</p>
</div>
<div class="content" [innerHTML]="text">
</div>
<div class="text-right">
<button class="btn btn-icon btn-simple"><i class="ti-comment"></i></button>
<button class="btn btn-icon btn-simple">
<i class="ti-heart"></i>
<span *ngIf="badge" class="badge">{{ badge }}</span>
</button>
<button class="btn btn-icon btn-simple"><i class="ti-share"></i></button>
</div>
</div>
</div>
</ng-template>
<ng-container *ngTemplateOutlet="followingpost;context:{author: 'Timothy', age: 2, badge: 18, text: 'Les Français qui payent plus de 2500 € d impôt peuvent bénéficier de cette loi : <a href=`http://bit.ly/2xxMicY`>http://bit.ly/2xxMicY</a>'}"></ng-container>
@boogie4eva
Copy link

Love it !. Saved my life didn't grab it earlier .

@jessycormier
Copy link

Is it possible to use the same ng-template for different containers that pass different contexts ?
also is it possible to manipulate the passed in content if its a property in the typescript component?

@dmushkov
Copy link

dmushkov commented Mar 1, 2018

You rock!

@vangtry
Copy link

vangtry commented Apr 9, 2018

Thanks man!! works perfect...

@nkanand4
Copy link

@jessycormier using $implicit context {$implicit: obj[field]}, you can use the same ng-template.

@AlyonaPavlova
Copy link

Oh my God! you saved me! thanks!

@davetheflashguy
Copy link

Thanks 3 years later this is still helping people

@m00zi
Copy link

m00zi commented Jul 19, 2020

Thanks, this is great example

@AnttiTanskanen
Copy link

Thanks fot this!

@HoangDinhHoi
Copy link

HoangDinhHoi commented Jun 14, 2021

You saved my life! Thanks you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment