-
-
Save belide/253b36c53fc278facf1baed952b4aa3e to your computer and use it in GitHub Desktop.
ng-template, ng-container and ngTemplateOutlet examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
selector: 'app-root', | |
template: ` | |
<ng-template> | |
<button class="tab-button" | |
(click)="login()">{{loginText}}</button> | |
<button class="tab-button" | |
(click)="signUp()">{{signUpText}}</button> | |
</ng-template> | |
`}) | |
export class AppComponent { | |
loginText = 'Login'; | |
signUpText = 'Sign Up'; | |
lessons = ['Lesson 1', 'Lessons 2']; | |
login() { | |
console.log('Login'); | |
} | |
signUp() { | |
console.log('Sign Up'); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="lessons-list" *ngIf="lessons else loading"> | |
... | |
</div> | |
<ng-template #loading> | |
<div>Loading...</div> | |
</ng-template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ng-template [ngIf]="lessons" [ngIfElse]="loading"> | |
<div class="lessons-list"> | |
... | |
</div> | |
</ng-template> | |
<ng-template #loading> | |
<div>Loading...</div> | |
</ng-template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="lesson" *ngIf="lessons" | |
*ngFor="let lesson of lessons"> | |
<div class="lesson-detail"> | |
{{lesson | json}} | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div *ngIf="lessons"> | |
<div class="lesson" *ngFor="let lesson of lessons"> | |
<div class="lesson-detail"> | |
{{lesson | json}} | |
</div> | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ng-container *ngIf="lessons"> | |
<div class="lesson" *ngFor="let lesson of lessons"> | |
<div class="lesson-detail"> | |
{{lesson | json}} | |
</div> | |
</div> | |
</ng-container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ng-container *ngTemplateOutlet="loading"></ng-container> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
selector: 'app-root', | |
template: ` | |
<ng-template #estimateTemplate let-lessonsCounter="estimate"> | |
<div> Approximately {{lessonsCounter}} lessons ...</div> | |
</ng-template> | |
<ng-container | |
*ngTemplateOutlet="estimateTemplate;context:ctx"> | |
</ng-container> | |
`}) | |
export class AppComponent { | |
totalEstimate = 10; | |
ctx = {estimate: this.totalEstimate}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
selector: 'app-root', | |
template: ` | |
<ng-template #defaultTabButtons> | |
<button class="tab-button" (click)="login()"> | |
{{loginText}} | |
</button> | |
<button class="tab-button" (click)="signUp()"> | |
{{signUpText}} | |
</button> | |
</ng-template> | |
`}) | |
export class AppComponent implements OnInit { | |
@ViewChild('defaultTabButtons') | |
private defaultTabButtonsTpl: TemplateRef<any>; | |
ngOnInit() { | |
console.log(this.defaultTabButtonsTpl); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
selector: 'app-root', | |
template: ` | |
<ng-template #customTabButtons> | |
<div class="custom-class"> | |
<button class="tab-button" (click)="login()"> | |
{{loginText}} | |
</button> | |
<button class="tab-button" (click)="signUp()"> | |
{{signUpText}} | |
</button> | |
</div> | |
</ng-template> | |
<tab-container [headerTemplate]="customTabButtons"></tab-container> | |
`}) | |
export class AppComponent implements OnInit { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component({ | |
selector: 'tab-container', | |
template: ` | |
<ng-template #defaultTabButtons> | |
<div class="default-tab-buttons"> | |
... | |
</div> | |
</ng-template> | |
<ng-container | |
*ngTemplateOutlet="headerTemplate ? headerTemplate: defaultTabButtons"> | |
</ng-container> | |
... rest of tab container component ... | |
`}) | |
export class TabContainerComponent { | |
@Input() | |
headerTemplate: TemplateRef<any>; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment