Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(core): add full example for IfLoadedDirective (#46425)
This commit adds a full example for the IfLoadedDirective and moves the code to the StackBlitz playground for structural directives. Fixes #40739, #37119 PR Close #46425
- Loading branch information
1 parent
639277b
commit 0ac2e54
Showing
6 changed files
with
84 additions
and
30 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
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
19 changes: 19 additions & 0 deletions
19
aio/content/examples/structural-directives/src/app/hero.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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Component } from '@angular/core'; | ||
|
||
import { LoadingState } from './loading-state'; | ||
import { Hero, heroes } from './hero'; | ||
|
||
@Component({ | ||
selector: 'app-hero', | ||
template: ` | ||
<button (click)="onLoadHero()">Load Hero</button> | ||
<p *appIfLoaded="heroLoadingState">{{ heroLoadingState.data | json }}</p> | ||
`, | ||
}) | ||
export class HeroComponent { | ||
heroLoadingState: LoadingState<Hero> = { type: 'loading' }; | ||
|
||
onLoadHero(): void { | ||
this.heroLoadingState = { type: 'loaded', data: heroes[0] }; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
aio/content/examples/structural-directives/src/app/if-loaded.directive.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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core'; | ||
|
||
import { Loaded, LoadingState } from './loading-state'; | ||
|
||
@Directive({ selector: '[appIfLoaded]' }) | ||
export class IfLoadedDirective<T> { | ||
private isViewCreated = false; | ||
|
||
@Input('appIfLoaded') set state(state: LoadingState<T>) { | ||
if (!this.isViewCreated && state.type === 'loaded') { | ||
this.viewContainerRef.createEmbeddedView(this.templateRef); | ||
this.isViewCreated = true; | ||
} else if (this.isViewCreated && state.type !== 'loaded') { | ||
this.viewContainerRef.clear(); | ||
this.isViewCreated = false; | ||
} | ||
} | ||
|
||
constructor( | ||
private readonly viewContainerRef: ViewContainerRef, | ||
private readonly templateRef: TemplateRef<unknown> | ||
) {} | ||
|
||
static ngTemplateGuard_appIfLoaded<T>( | ||
dir: IfLoadedDirective<T>, | ||
state: LoadingState<T> | ||
): state is Loaded<T> { | ||
return true; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
aio/content/examples/structural-directives/src/app/loading-state.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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export type Loaded<T> = { type: 'loaded', data: T }; | ||
|
||
export type Loading = { type: 'loading' }; | ||
|
||
export type LoadingState<T> = Loaded<T> | Loading; |
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