docs(aio): update migrated content from anguar.io
This commit is contained in:

committed by
Pete Bacon Darwin

parent
ff82756415
commit
fd72fad8fd
@ -0,0 +1,70 @@
|
||||
/* #docregion */
|
||||
button {
|
||||
min-width: 100px;
|
||||
font-size: 100%;
|
||||
}
|
||||
|
||||
.box {
|
||||
border: 1px solid gray;
|
||||
max-width: 600px;
|
||||
padding: 4px;
|
||||
}
|
||||
.choices {
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
code, .code {
|
||||
background-color: #eee;
|
||||
color: black;
|
||||
font-family: Courier, sans-serif;
|
||||
font-size: 85%;
|
||||
}
|
||||
|
||||
div.code {
|
||||
width: 400px;
|
||||
}
|
||||
|
||||
.heroic {
|
||||
font-size: 150%;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
hr {
|
||||
margin: 40px 0
|
||||
}
|
||||
|
||||
.odd {
|
||||
background-color: palegoldenrod;
|
||||
}
|
||||
|
||||
td, th {
|
||||
text-align: left;
|
||||
vertical-align: top;
|
||||
}
|
||||
|
||||
/* #docregion p-span */
|
||||
p span { color: red; font-size: 70%; }
|
||||
/* #enddocregion p-span */
|
||||
|
||||
.unless {
|
||||
border: 2px solid;
|
||||
padding: 6px;
|
||||
}
|
||||
|
||||
p.unless {
|
||||
width: 500px;
|
||||
}
|
||||
|
||||
button.a, span.a, .unless.a {
|
||||
color: red;
|
||||
border-color: gold;
|
||||
background-color: yellow;
|
||||
font-size: 100%;
|
||||
}
|
||||
|
||||
button.b, span.b, .unless.b {
|
||||
color: black;
|
||||
border-color: green;
|
||||
background-color: lightgreen;
|
||||
font-size: 100%;
|
||||
}
|
@ -0,0 +1,250 @@
|
||||
<!-- #docplaster -->
|
||||
<!-- #docregion -->
|
||||
<h1>Structural Directives</h1>
|
||||
|
||||
<p>Conditional display of hero</p>
|
||||
|
||||
<blockquote>
|
||||
<!-- #docregion built-in, asterisk, ngif -->
|
||||
<div *ngIf="hero" >{{hero.name}}</div>
|
||||
<!-- #enddocregion built-in, asterisk, ngif -->
|
||||
</blockquote>
|
||||
|
||||
<p>List of heroes</p>
|
||||
<!-- #docregion built-in -->
|
||||
|
||||
<ul>
|
||||
<!-- #docregion ngfor-li -->
|
||||
<li *ngFor="let hero of heroes">{{hero.name}}</li>
|
||||
<!-- #enddocregion ngfor-li -->
|
||||
</ul>
|
||||
|
||||
<!-- #enddocregion built-in -->
|
||||
|
||||
<hr>
|
||||
|
||||
<h2 id="ngIf">NgIf</h2>
|
||||
|
||||
<!-- #docregion ngif-true -->
|
||||
<p *ngIf="true">
|
||||
Expression is true and ngIf is true.
|
||||
This paragraph is in the DOM.
|
||||
</p>
|
||||
<p *ngIf="false">
|
||||
Expression is false and ngIf is false.
|
||||
This paragraph is not in the DOM.
|
||||
</p>
|
||||
<!-- #enddocregion ngif-true -->
|
||||
|
||||
<!-- #docregion display-none -->
|
||||
<p [style.display]="'block'">
|
||||
Expression sets display to "block".
|
||||
This paragraph is visible.
|
||||
</p>
|
||||
<p [style.display]="'none'">
|
||||
Expression sets display to "none".
|
||||
This paragraph is hidden but still in the DOM.
|
||||
</p>
|
||||
<!-- #enddocregion display-none -->
|
||||
|
||||
<h4>NgIf with template</h4>
|
||||
<p><template> element</p>
|
||||
<!-- #docregion ngif-template -->
|
||||
<template [ngIf]="hero">
|
||||
<div>{{hero.name}}</div>
|
||||
</template>
|
||||
<!-- #enddocregion ngif-template -->
|
||||
|
||||
<p>template attribute</p>
|
||||
<!-- #docregion ngif-template-attr -->
|
||||
<div template="ngIf hero">{{hero.name}}</div>
|
||||
<!-- #enddocregion ngif-template-attr -->
|
||||
|
||||
<hr>
|
||||
|
||||
<h2 id="ng-container"><ng-container></h2>
|
||||
|
||||
<h4>*ngIf with a <ng-container></h4>
|
||||
|
||||
<button (click)="hero = hero ? null : heroes[0]">Toggle hero</button>
|
||||
|
||||
<!-- #docregion ngif-ngcontainer -->
|
||||
<p>
|
||||
I turned the corner
|
||||
<ng-container *ngIf="hero">
|
||||
and saw {{hero.name}}. I waved
|
||||
</ng-container>
|
||||
and continued on my way.
|
||||
</p>
|
||||
<!-- #enddocregion ngif-ngcontainer -->
|
||||
<!-- #docregion ngif-span -->
|
||||
<p>
|
||||
I turned the corner
|
||||
<span *ngIf="hero">
|
||||
and saw {{hero.name}}. I waved
|
||||
</span>
|
||||
and continued on my way.
|
||||
</p>
|
||||
<!-- #enddocregion ngif-span -->
|
||||
|
||||
<p><i><select> with <span></i></p>
|
||||
<!-- #docregion select-span -->
|
||||
<div>
|
||||
Pick your favorite hero
|
||||
(<label><input type="checkbox" checked (change)="showSad = !showSad">show sad</label>)
|
||||
</div>
|
||||
<select [(ngModel)]="hero">
|
||||
<span *ngFor="let h of heroes">
|
||||
<span *ngIf="showSad || h.emotion !== 'sad'">
|
||||
<option [ngValue]="h">{{h.name}} ({{h.emotion}})</option>
|
||||
</span>
|
||||
</span>
|
||||
</select>
|
||||
<!-- #enddocregion select-span -->
|
||||
|
||||
<p><i><select> with <ng-container></i></p>
|
||||
<!-- #docregion select-ngcontainer -->
|
||||
<div>
|
||||
Pick your favorite hero
|
||||
(<label><input type="checkbox" checked (change)="showSad = !showSad">show sad</label>)
|
||||
</div>
|
||||
<select [(ngModel)]="hero">
|
||||
<ng-container *ngFor="let h of heroes">
|
||||
<ng-container *ngIf="showSad || h.emotion !== 'sad'">
|
||||
<option [ngValue]="h">{{h.name}} ({{h.emotion}})</option>
|
||||
</ng-container>
|
||||
</ng-container>
|
||||
</select>
|
||||
<!-- #enddocregion select-ngcontainer -->
|
||||
<br><br>
|
||||
|
||||
<hr>
|
||||
|
||||
<h2 id="ngFor">NgFor</h2>
|
||||
|
||||
<div class="box">
|
||||
|
||||
<p class="code"><div *ngFor="let hero of heroes; let i=index; let odd=odd; trackBy: trackById" [class.odd]="odd"></p>
|
||||
<!--#docregion inside-ngfor -->
|
||||
<div *ngFor="let hero of heroes; let i=index; let odd=odd; trackBy: trackById" [class.odd]="odd">
|
||||
({{i}}) {{hero.name}}
|
||||
</div>
|
||||
|
||||
<!--#enddocregion inside-ngfor -->
|
||||
<p class="code"><div template="ngFor let hero of heroes; let i=index; let odd=odd; trackBy: trackById" [class.odd]="odd"></p>
|
||||
<!--#docregion inside-ngfor -->
|
||||
<div template="ngFor let hero of heroes; let i=index; let odd=odd; trackBy: trackById" [class.odd]="odd">
|
||||
({{i}}) {{hero.name}}
|
||||
</div>
|
||||
|
||||
<!--#enddocregion inside-ngfor -->
|
||||
<p class="code"><template ngFor let-hero [ngForOf]="heroes" let-i="index" let-odd="odd" [ngForTrackBy]="trackById"></p>
|
||||
<!--#docregion inside-ngfor -->
|
||||
<template ngFor let-hero [ngForOf]="heroes" let-i="index" let-odd="odd" [ngForTrackBy]="trackById">
|
||||
<div [class.odd]="odd">({{i}}) {{hero.name}}</div>
|
||||
</template>
|
||||
<!--#enddocregion inside-ngfor -->
|
||||
|
||||
</div>
|
||||
<hr>
|
||||
|
||||
<h2 id="ngSwitch">NgSwitch</h2>
|
||||
|
||||
<div>Pick your favorite hero</div>
|
||||
<p>
|
||||
<label *ngFor="let h of heroes">
|
||||
<input type="radio" name="heroes" [(ngModel)]="hero" [value]="h">{{h.name}}
|
||||
</label>
|
||||
<label><input type="radio" name="heroes" (click)="hero = null">None of the above</label>
|
||||
</p>
|
||||
|
||||
<h4>NgSwitch</h4>
|
||||
|
||||
<!-- #docregion built-in , ngswitch -->
|
||||
<div [ngSwitch]="hero?.emotion">
|
||||
<happy-hero *ngSwitchCase="'happy'" [hero]="hero"></happy-hero>
|
||||
<sad-hero *ngSwitchCase="'sad'" [hero]="hero"></sad-hero>
|
||||
<confused-hero *ngSwitchCase="'confused'" [hero]="hero"></confused-hero>
|
||||
<unknown-hero *ngSwitchDefault [hero]="hero"></unknown-hero>
|
||||
</div>
|
||||
<!-- #enddocregion built-in, ngswitch -->
|
||||
|
||||
<h4>NgSwitch with <i>template</i> attribute</h4>
|
||||
<!-- #docregion ngswitch-template-attr -->
|
||||
<div [ngSwitch]="hero?.emotion">
|
||||
<happy-hero template="ngSwitchCase 'happy'" [hero]="hero"></happy-hero>
|
||||
<sad-hero template="ngSwitchCase 'sad'" [hero]="hero"></sad-hero>
|
||||
<confused-hero template="ngSwitchCase 'confused'" [hero]="hero"></confused-hero>
|
||||
<unknown-hero template="ngSwitchDefault" [hero]="hero"></unknown-hero>
|
||||
</div>
|
||||
<!-- #enddocregion ngswitch-template-attr -->
|
||||
|
||||
<h4>NgSwitch with <template></h4>
|
||||
<!-- #docregion ngswitch-template -->
|
||||
<div [ngSwitch]="hero?.emotion">
|
||||
<template [ngSwitchCase]="'happy'">
|
||||
<happy-hero [hero]="hero"></happy-hero>
|
||||
</template>
|
||||
<template [ngSwitchCase]="'sad'">
|
||||
<sad-hero [hero]="hero"></sad-hero>
|
||||
</template>
|
||||
<template [ngSwitchCase]="'confused'">
|
||||
<confused-hero [hero]="hero"></confused-hero>
|
||||
</template >
|
||||
<template ngSwitchDefault>
|
||||
<unknown-hero [hero]="hero"></unknown-hero>
|
||||
</template>
|
||||
</div>
|
||||
<!-- #enddocregion ngswitch-template -->
|
||||
|
||||
<hr>
|
||||
|
||||
<h2><template></h2>
|
||||
<!-- #docregion template-tag -->
|
||||
<p>Hip!</p>
|
||||
<template>
|
||||
<p>Hip!</p>
|
||||
</template>
|
||||
<p>Hooray!</p>
|
||||
<!-- #enddocregion template-tag -->
|
||||
|
||||
<hr>
|
||||
|
||||
<h2 id="myUnless">UnlessDirective</h2>
|
||||
<p>
|
||||
The condition is currently
|
||||
<span [ngClass]="{ a: !condition, b: condition, unless: true }">{{condition}}</span>.
|
||||
<button
|
||||
(click)="condition = !condition"
|
||||
[ngClass] = "{ a: condition, b: !condition }" >
|
||||
Toggle condition to {{condition ? 'false' : 'true'}}
|
||||
</button>
|
||||
</p>
|
||||
<!-- #docregion myUnless-->
|
||||
<p *myUnless="condition" class="unless a">
|
||||
(A) This paragraph is displayed because the condition is false.
|
||||
</p>
|
||||
|
||||
<p *myUnless="!condition" class="unless b">
|
||||
(B) Although the condition is true,
|
||||
this paragraph is displayed because myUnless is set to false.
|
||||
</p>
|
||||
<!-- #enddocregion myUnless-->
|
||||
|
||||
|
||||
<h4>UnlessDirective with template</h4>
|
||||
|
||||
<!-- #docregion myUnless-1 -->
|
||||
<p *myUnless="condition">Show this sentence unless the condition is true.</p>
|
||||
<!-- #enddocregion myUnless-1 -->
|
||||
|
||||
<p template="myUnless condition" class="code unless">
|
||||
(A) <p template="myUnless condition" class="code unless">
|
||||
</p>
|
||||
|
||||
<template [myUnless]="condition">
|
||||
<p class="code unless">
|
||||
(A) <template [myUnless]="condition">
|
||||
</p>
|
||||
</template>
|
||||
|
@ -0,0 +1,23 @@
|
||||
// #docregion
|
||||
import { Component } from '@angular/core';
|
||||
|
||||
import { Hero, heroes } from './hero';
|
||||
|
||||
@Component({
|
||||
selector: 'my-app',
|
||||
templateUrl: './app.component.html',
|
||||
styleUrls: [ './app.component.css' ]
|
||||
})
|
||||
export class AppComponent {
|
||||
heroes = heroes;
|
||||
hero = this.heroes[0];
|
||||
|
||||
condition = false;
|
||||
logs: string[] = [];
|
||||
showSad = true;
|
||||
status = 'ready';
|
||||
|
||||
// #docregion trackByHero
|
||||
trackById(index: number, hero: Hero): number { return hero.id; }
|
||||
// #enddocregion trackByHero
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
// #docregion
|
||||
import { NgModule } from '@angular/core';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
import { BrowserModule } from '@angular/platform-browser';
|
||||
|
||||
import { AppComponent } from './app.component';
|
||||
import { heroSwitchComponents } from './hero-switch.components';
|
||||
import { UnlessDirective } from './unless.directive';
|
||||
|
||||
@NgModule({
|
||||
imports: [ BrowserModule, FormsModule ],
|
||||
declarations: [
|
||||
AppComponent,
|
||||
heroSwitchComponents,
|
||||
UnlessDirective
|
||||
],
|
||||
bootstrap: [ AppComponent ]
|
||||
})
|
||||
export class AppModule { }
|
@ -0,0 +1,43 @@
|
||||
// #docregion
|
||||
import { Component, Input } from '@angular/core';
|
||||
import { Hero } from './hero';
|
||||
|
||||
@Component({
|
||||
selector: 'happy-hero',
|
||||
template: `Wow. You like {{hero.name}}. What a happy hero ... just like you.`
|
||||
})
|
||||
export class HappyHeroComponent {
|
||||
@Input() hero: Hero;
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'sad-hero',
|
||||
template: `You like {{hero.name}}? Such a sad hero. Are you sad too?`
|
||||
})
|
||||
export class SadHeroComponent {
|
||||
@Input() hero: Hero;
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'confused-hero',
|
||||
template: `Are you as confused as {{hero.name}}?`
|
||||
})
|
||||
export class ConfusedHeroComponent {
|
||||
@Input() hero: Hero;
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'unknown-hero',
|
||||
template: `{{message}}`
|
||||
})
|
||||
export class UnknownHeroComponent {
|
||||
@Input() hero: Hero;
|
||||
get message() {
|
||||
return this.hero && this.hero.name ?
|
||||
`${this.hero.name} is strange and mysterious.` :
|
||||
'Are you feeling indecisive?';
|
||||
}
|
||||
}
|
||||
|
||||
export const heroSwitchComponents =
|
||||
[ HappyHeroComponent, SadHeroComponent, ConfusedHeroComponent, UnknownHeroComponent ];
|
13
aio/content/examples/structural-directives/src/app/hero.ts
Normal file
13
aio/content/examples/structural-directives/src/app/hero.ts
Normal file
@ -0,0 +1,13 @@
|
||||
// #docregion
|
||||
export class Hero {
|
||||
id: number;
|
||||
name: string;
|
||||
emotion?: string;
|
||||
}
|
||||
|
||||
export const heroes: Hero[] = [
|
||||
{ id: 1, name: 'Mr. Nice', emotion: 'happy'},
|
||||
{ id: 2, name: 'Narco', emotion: 'sad' },
|
||||
{ id: 3, name: 'Windstorm', emotion: 'confused' },
|
||||
{ id: 4, name: 'Magneta'}
|
||||
];
|
21
aio/content/examples/structural-directives/src/app/scrap.txt
Normal file
21
aio/content/examples/structural-directives/src/app/scrap.txt
Normal file
@ -0,0 +1,21 @@
|
||||
// interesting but unused code
|
||||
heroChooser(picker: HTMLFieldSetElement) {
|
||||
let choices = picker.children;
|
||||
this.favoriteHero = undefined;
|
||||
for (let i = 0; i < choices.length; i++) {
|
||||
let choice = choices[i].children[0] as HTMLInputElement;
|
||||
if (choice.checked) { this.favoriteHero = this.heroes[i]; }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
<h4>Switch with *ngFor repeated switchCases using <ng-container></h4>
|
||||
<!-- #docregion NgSwitch-ngFor -->
|
||||
<div [ngSwitch]="hero.id">
|
||||
Your favorite hero is ...
|
||||
<ng-container *ngFor="let hero of heroes">
|
||||
<ng-container *ngSwitchCase="hero.id">{{hero.name}}</ng-container>
|
||||
</ng-container>
|
||||
<ng-container *ngSwitchDefault>None of the above</ng-container>
|
||||
</div>
|
||||
<!-- #enddocregion NgSwitch-ngFor -->
|
@ -0,0 +1,51 @@
|
||||
// #docplaster
|
||||
// #docregion
|
||||
// #docregion no-docs, skeleton
|
||||
import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core';
|
||||
|
||||
// #enddocregion skeleton
|
||||
/**
|
||||
* Add the template content to the DOM unless the condition is true.
|
||||
// #enddocregion no-docs
|
||||
*
|
||||
* If the expression assigned to `myUnless` evaluates to a truthy value
|
||||
* then the templated elements are removed removed from the DOM,
|
||||
* the templated elements are (re)inserted into the DOM.
|
||||
*
|
||||
* <div *ngUnless="errorCount" class="success">
|
||||
* Congrats! Everything is great!
|
||||
* </div>
|
||||
*
|
||||
* ### Syntax
|
||||
*
|
||||
* - `<div *myUnless="condition">...</div>`
|
||||
* - `<div template="myUnless condition">...</div>`
|
||||
* - `<template [myUnless]="condition"><div>...</div></template>`
|
||||
*
|
||||
// #docregion no-docs
|
||||
*/
|
||||
// #docregion skeleton
|
||||
@Directive({ selector: '[myUnless]'})
|
||||
export class UnlessDirective {
|
||||
// #enddocregion skeleton
|
||||
private hasView = false;
|
||||
|
||||
// #docregion ctor
|
||||
constructor(
|
||||
private templateRef: TemplateRef<any>,
|
||||
private viewContainer: ViewContainerRef) { }
|
||||
// #enddocregion ctor
|
||||
|
||||
// #docregion set
|
||||
@Input() set myUnless(condition: boolean) {
|
||||
if (!condition && !this.hasView) {
|
||||
this.viewContainer.createEmbeddedView(this.templateRef);
|
||||
this.hasView = true;
|
||||
} else if (condition && this.hasView) {
|
||||
this.viewContainer.clear();
|
||||
this.hasView = false;
|
||||
}
|
||||
}
|
||||
// #enddocregion set
|
||||
// #docregion skeleton
|
||||
}
|
27
aio/content/examples/structural-directives/src/index.html
Normal file
27
aio/content/examples/structural-directives/src/index.html
Normal file
@ -0,0 +1,27 @@
|
||||
<!DOCTYPE html>
|
||||
<!-- #docregion -->
|
||||
<html>
|
||||
<head>
|
||||
<title>Angular Structural Directives</title>
|
||||
<base href="/">
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="stylesheet" href="styles.css">
|
||||
|
||||
<!-- Polyfills -->
|
||||
<script src="node_modules/core-js/client/shim.min.js"></script>
|
||||
|
||||
<script src="node_modules/zone.js/dist/zone.js"></script>
|
||||
<script src="node_modules/systemjs/dist/system.src.js"></script>
|
||||
|
||||
<script src="systemjs.config.js"></script>
|
||||
<script>
|
||||
System.import('main.js').catch(function(err){ console.error(err); });
|
||||
</script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<my-app>Loading...</my-apps>
|
||||
</body>
|
||||
|
||||
</html>
|
6
aio/content/examples/structural-directives/src/main.ts
Normal file
6
aio/content/examples/structural-directives/src/main.ts
Normal file
@ -0,0 +1,6 @@
|
||||
// #docregion
|
||||
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
|
||||
import { AppModule } from './app/app.module';
|
||||
|
||||
platformBrowserDynamic().bootstrapModule(AppModule);
|
||||
|
Reference in New Issue
Block a user