Professional Documents
Culture Documents
See the live example / download example of the code in this cookbook.
components and insert them into the current view. This directive allows
@Component({
selector: 'app-ad-banner',
standalone: ,
imports: [ , ],
: `
<div class="ad-banner-example">
<h3>Advertisements<∕h3>
<ng-container *ngComponentOutlet="
currentAd.component;
inputs: currentAd.inputs;
" ∕>
<button (click)="displayNextAd()">Next<∕button>
<∕div>
`
})
{
adList = inject( ).getAds();
currentAdIndex = 0;
currentAd() {
.adList[ .currentAdIndex];
}
displayNextAd() {
.currentAdIndex++;
∕∕ Reset the current ad index back to `0` when we
reach the end of an array.
( .currentAdIndex === .adList.length) {
.currentAdIndex = 0;
}
}
}
indicate that the �rst ad should be displayed. When a user clicks the
"Next" button, the index is increased by one. Once the index reaches the
the value changes, Angular picks it up and re�ects the changes in the UI.
Here are two sample components and the service providing them with
their inputs:
{ , } '@angular∕core';
@Component({
standalone: ,
: `
<div class="job-ad">
<h4>{{ headline }}<∕h4>
{{ body }}
<∕div>
`,
})
{
@Input() headline!: ;
@Input() body!: ;
}
Final ad banner
The �nal ad banner looks like this: