forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: update Dynamic Component loader docs to rely on `ngComponentOut…
…let` With angular#51148, the `ngComponentOutlet` directive now supports inputs. This allows a less verbose and simpler API to instantiate components dynamicaly. Fixes angular#49875
- Loading branch information
Showing
14 changed files
with
83 additions
and
215 deletions.
There are no files selected for viewing
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
55 changes: 22 additions & 33 deletions
55
aio/content/examples/dynamic-component-loader/src/app/ad-banner.component.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,45 @@ | ||
// #docregion | ||
import { Component, Input, OnDestroy, OnInit, ViewChild } from '@angular/core'; | ||
import { Component, inject, OnDestroy, OnInit, } from '@angular/core'; | ||
import { CommonModule, NgComponentOutlet } from '@angular/common'; | ||
|
||
import { AdDirective } from './ad.directive'; | ||
import { AdItem } from './ad-item'; | ||
import { AdComponent } from './ad.component'; | ||
import { AdService } from './ad.service'; | ||
|
||
@Component({ | ||
selector: 'app-ad-banner', | ||
// #docregion ad-host | ||
standalone: true, | ||
imports: [CommonModule, NgComponentOutlet], | ||
// #docregion ng-container | ||
template: ` | ||
<div class="ad-banner-example"> | ||
<h3>Advertisements</h3> | ||
<ng-template adHost></ng-template> | ||
<ng-container *ngComponentOutlet=" | ||
currentAd.component; | ||
inputs: currentAd.inputs; | ||
" /> | ||
</div> | ||
` | ||
// #enddocregion ad-host | ||
// #enddocregion ng-container | ||
}) | ||
// #docregion class | ||
export class AdBannerComponent implements OnInit, OnDestroy { | ||
@Input() ads: AdItem[] = []; | ||
adList = inject(AdService).getAds(); | ||
|
||
currentAdIndex = -1; | ||
currentAdIndex = 0; | ||
currentAd = this.adList[this.currentAdIndex]; | ||
|
||
@ViewChild(AdDirective, {static: true}) adHost!: AdDirective; | ||
private interval!: number; | ||
|
||
private clearTimer: VoidFunction | undefined; | ||
|
||
ngOnInit(): void { | ||
this.loadComponent(); | ||
this.getAds(); | ||
ngOnInit() { | ||
this.interval = window.setInterval(() => this.displayNextAd(), 2000); | ||
} | ||
|
||
ngOnDestroy() { | ||
this.clearTimer?.(); | ||
displayNextAd() { | ||
this.currentAdIndex = (this.currentAdIndex + 1) % this.adList.length; | ||
this.currentAd = this.adList[this.currentAdIndex]; | ||
} | ||
|
||
loadComponent() { | ||
this.currentAdIndex = (this.currentAdIndex + 1) % this.ads.length; | ||
const adItem = this.ads[this.currentAdIndex]; | ||
|
||
const viewContainerRef = this.adHost.viewContainerRef; | ||
viewContainerRef.clear(); | ||
|
||
const componentRef = viewContainerRef.createComponent<AdComponent>(adItem.component); | ||
componentRef.instance.data = adItem.data; | ||
} | ||
|
||
getAds() { | ||
const interval = setInterval(() => { | ||
this.loadComponent(); | ||
}, 3000); | ||
this.clearTimer = () => clearInterval(interval); | ||
ngOnDestroy() { | ||
clearInterval(this.interval); | ||
} | ||
} | ||
// #enddocregion class |
6 changes: 0 additions & 6 deletions
6
aio/content/examples/dynamic-component-loader/src/app/ad-item.ts
This file was deleted.
Oops, something went wrong.
4 changes: 0 additions & 4 deletions
4
aio/content/examples/dynamic-component-loader/src/app/ad.component.ts
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
aio/content/examples/dynamic-component-loader/src/app/ad.directive.ts
This file was deleted.
Oops, something went wrong.
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
24 changes: 0 additions & 24 deletions
24
aio/content/examples/dynamic-component-loader/src/app/app.component.ts
This file was deleted.
Oops, something went wrong.
23 changes: 0 additions & 23 deletions
23
aio/content/examples/dynamic-component-loader/src/app/app.module.ts
This file was deleted.
Oops, something went wrong.
16 changes: 8 additions & 8 deletions
16
aio/content/examples/dynamic-component-loader/src/app/hero-job-ad.component.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
/* eslint-disable @angular-eslint/no-input-rename */ | ||
// #docregion | ||
import { Component, Input } from '@angular/core'; | ||
|
||
import { AdComponent } from './ad.component'; | ||
|
||
@Component({ | ||
standalone: true, | ||
template: ` | ||
<div class="job-ad"> | ||
<h4>{{data.headline}}</h4> | ||
{{data.body}} | ||
<h4>{{ headline }}</h4> | ||
{{ body }} | ||
</div> | ||
` | ||
`, | ||
}) | ||
export class HeroJobAdComponent implements AdComponent { | ||
@Input() data: any; | ||
export class HeroJobAdComponent { | ||
@Input({ required: true }) headline!: string; | ||
@Input({ required: true }) body!: string; | ||
} | ||
|
19 changes: 8 additions & 11 deletions
19
aio/content/examples/dynamic-component-loader/src/app/hero-profile.component.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,19 @@ | ||
/* eslint-disable @angular-eslint/no-input-rename */ | ||
// #docregion | ||
import { Component, Input } from '@angular/core'; | ||
|
||
import { AdComponent } from './ad.component'; | ||
|
||
@Component({ | ||
standalone: true, | ||
template: ` | ||
<div class="hero-profile"> | ||
<h3>Featured Hero Profile</h3> | ||
<h4>{{data.name}}</h4> | ||
<p>{{data.bio}}</p> | ||
<h4>{{ name }}</h4> | ||
<p>{{ bio }}</p> | ||
<strong>Hire this hero today!</strong> | ||
</div> | ||
` | ||
`, | ||
}) | ||
export class HeroProfileComponent implements AdComponent { | ||
@Input() data: any; | ||
export class HeroProfileComponent { | ||
@Input({ required: true }) name!: string; | ||
@Input({ required: true }) bio!: string; | ||
} | ||
|
||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
// #docregion | ||
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; | ||
import { bootstrapApplication } from '@angular/platform-browser'; | ||
import { AdBannerComponent } from './app/ad-banner.component'; | ||
|
||
import { AppModule } from './app/app.module'; | ||
|
||
platformBrowserDynamic().bootstrapModule(AppModule) | ||
.catch(err => console.error(err)); | ||
bootstrapApplication(AdBannerComponent); |
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
Oops, something went wrong.