angular-cn/aio/content/examples/structural-directives/ts/plnkr.no-link.html

612 lines
17 KiB
HTML

<html lang="en"><head></head><body><form id="mainForm" method="post" action="http://plnkr.co/edit/?p=preview" target="_self"><input type="hidden" name="files[app/app.component.ts]" value="import { Component } from '@angular/core';
import { Hero, heroes } from './hero';
@Component({
moduleId: module.id,
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';
trackById(index: number, hero: Hero): number { return hero.id; }
}
/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/"><input type="hidden" name="files[app/app.module.ts]" value="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 { }
/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/"><input type="hidden" name="files[app/hero-switch.components.ts]" value="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 &amp;&amp; this.hero.name ?
`${this.hero.name} is strange and mysterious.` :
'Are you feeling indecisive?';
}
}
export const heroSwitchComponents =
[ HappyHeroComponent, SadHeroComponent, ConfusedHeroComponent, UnknownHeroComponent ];
/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/"><input type="hidden" name="files[app/hero.ts]" value="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'}
];
/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/"><input type="hidden" name="files[app/unless.directive.ts]" value="import { Directive, Input, TemplateRef, ViewContainerRef } from '@angular/core';
/**
* Add the template content to the DOM unless the condition is true.
*
* 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=&quot;errorCount&quot; class=&quot;success&quot;>
* Congrats! Everything is great!
* </div>
*
* ### Syntax
* *
* - `<div *myUnless=&quot;condition&quot;>...</div>`
* - `<div template=&quot;myUnless condition&quot;>...</div>`
* - `<template [myUnless]=&quot;condition&quot;><div>...</div></template>`
*
*/
@Directive({ selector: '[myUnless]'})
export class UnlessDirective {
private hasView = false;
constructor(
private templateRef: TemplateRef<any>,
private viewContainer: ViewContainerRef) { }
@Input() set myUnless(condition: boolean) {
if (!condition &amp;&amp; !this.hasView) {
this.viewContainer.createEmbeddedView(this.templateRef);
this.hasView = true;
} else if (condition &amp;&amp; this.hasView) {
this.viewContainer.clear();
this.hasView = false;
}
}
}
/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/"><input type="hidden" name="files[main.ts]" value="import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
platformBrowserDynamic().bootstrapModule(AppModule);
/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/"><input type="hidden" name="files[app/app.component.css]" value="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;
}
p span { color: red; font-size: 70%; }
.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%;
}
/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/"><input type="hidden" name="files[styles.css]" value="/* Master Styles */
h1 {
color: #369;
font-family: Arial, Helvetica, sans-serif;
font-size: 250%;
}
h2, h3 {
color: #444;
font-family: Arial, Helvetica, sans-serif;
font-weight: lighter;
}
body {
margin: 2em;
}
body, input[text], button {
color: #888;
font-family: Cambria, Georgia;
}
a {
cursor: pointer;
cursor: hand;
}
button {
font-family: Arial;
background-color: #eee;
border: none;
padding: 5px 10px;
border-radius: 4px;
cursor: pointer;
cursor: hand;
}
button:hover {
background-color: #cfd8dc;
}
button:disabled {
background-color: #eee;
color: #aaa;
cursor: auto;
}
/* Navigation link styles */
nav a {
padding: 5px 10px;
text-decoration: none;
margin-right: 10px;
margin-top: 10px;
display: inline-block;
background-color: #eee;
border-radius: 4px;
}
nav a:visited, a:link {
color: #607D8B;
}
nav a:hover {
color: #039be5;
background-color: #CFD8DC;
}
nav a.active {
color: #039be5;
}
/* items class */
.items {
margin: 0 0 2em 0;
list-style-type: none;
padding: 0;
width: 24em;
}
.items li {
cursor: pointer;
position: relative;
left: 0;
background-color: #EEE;
margin: .5em;
padding: .3em 0;
height: 1.6em;
border-radius: 4px;
}
.items li:hover {
color: #607D8B;
background-color: #DDD;
left: .1em;
}
.items li.selected {
background-color: #CFD8DC;
color: white;
}
.items li.selected:hover {
background-color: #BBD8DC;
}
.items .text {
position: relative;
top: -3px;
}
.items .badge {
display: inline-block;
font-size: small;
color: white;
padding: 0.8em 0.7em 0 0.7em;
background-color: #607D8B;
line-height: 1em;
position: relative;
left: -1px;
top: -4px;
height: 1.8em;
margin-right: .8em;
border-radius: 4px 0 0 4px;
}
/* everywhere else */
* {
font-family: Arial, Helvetica, sans-serif;
}
/*
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
*/"><input type="hidden" name="files[app/app.component.html]" value="<h1>Structural Directives</h1>
<p>Conditional display of hero</p>
<blockquote>
<div *ngIf=&quot;hero&quot; >{{hero.name}}</div>
</blockquote>
<p>List of heroes</p>
<ul>
<li *ngFor=&quot;let hero of heroes&quot;>{{hero.name}}</li>
</ul>
<hr>
<h2 id=&quot;ngIf&quot;>NgIf</h2>
<p *ngIf=&quot;true&quot;>
Expression is true and ngIf is true.
This paragraph is in the DOM.
</p>
<p *ngIf=&quot;false&quot;>
Expression is false and ngIf is false.
This paragraph is not in the DOM.
</p>
<p [style.display]=&quot;'block'&quot;>
Expression sets display to &quot;block&quot;&quot; .
This paragraph is visible.
</p>
<p [style.display]=&quot;'none'&quot;>
Expression sets display to &quot;none&quot; .
This paragraph is hidden but still in the DOM.
</p>
<h4>NgIf with template</h4>
<p>&amp;lt;template&amp;gt; element</p>
<template [ngIf]=&quot;hero&quot;>
<div>{{hero.name}}</div>
</template>
<p>template attribute</p>
<div template=&quot;ngIf hero&quot;>{{hero.name}}</div>
<hr>
<h2 id=&quot;ng-container&quot;>&amp;lt;ng-container&amp;gt;</h2>
<h4>*ngIf with a &amp;lt;ng-container&amp;gt;</h4>
<button (click)=&quot;hero = hero ? null : heroes[0]&quot;>Toggle hero</button>
<p>
I turned the corner
<ng-container *ngIf=&quot;hero&quot;>
and saw {{hero.name}}. I waved
</ng-container>
and continued on my way.
</p>
<p>
I turned the corner
<span *ngIf=&quot;hero&quot;>
and saw {{hero.name}}. I waved
</span>
and continued on my way.
</p>
<p><i>&amp;lt;select&amp;gt; with &amp;lt;span&amp;gt;</i></p>
<div>
Pick your favorite hero
(<label><input type=&quot;checkbox&quot; checked (change)=&quot;showSad = !showSad&quot;>show sad</label>)
</div>
<select [(ngModel)]=&quot;hero&quot;>
<span *ngFor=&quot;let h of heroes&quot;>
<span *ngIf=&quot;showSad || h.emotion !== 'sad'&quot;>
<option [ngValue]=&quot;h&quot;>{{h.name}} ({{h.emotion}})</option>
</span>
</span>
</select>
<p><i>&amp;lt;select&amp;gt; with &amp;lt;ng-container&amp;gt;</i></p>
<div>
Pick your favorite hero
(<label><input type=&quot;checkbox&quot; checked (change)=&quot;showSad = !showSad&quot;>show sad</label>)
</div>
<select [(ngModel)]=&quot;hero&quot;>
<ng-container *ngFor=&quot;let h of heroes&quot;>
<ng-container *ngIf=&quot;showSad || h.emotion !== 'sad'&quot;>
<option [ngValue]=&quot;h&quot;>{{h.name}} ({{h.emotion}})</option>
</ng-container>
</ng-container>
</select>
<br><br>
<hr>
<h2 id=&quot;ngFor&quot;>NgFor</h2>
<div class=&quot;box&quot;>
<p class=&quot;code&quot;>&amp;lt;div *ngFor=&quot;let hero of heroes; let i=index; let odd=odd; trackBy: trackById&quot; [class.odd]=&quot;odd&quot;&amp;gt;</p>
<div *ngFor=&quot;let hero of heroes; let i=index; let odd=odd; trackBy: trackById&quot; [class.odd]=&quot;odd&quot;>
({{i}}) {{hero.name}}
</div>
<p class=&quot;code&quot;>&amp;lt;div template=&quot;ngFor let hero of heroes; let i=index; let odd=odd; trackBy: trackById&quot; [class.odd]=&quot;odd&quot;&amp;gt;</p>
<div template=&quot;ngFor let hero of heroes; let i=index; let odd=odd; trackBy: trackById&quot; [class.odd]=&quot;odd&quot;>
({{i}}) {{hero.name}}
</div>
<p class=&quot;code&quot;>&amp;lt;template ngFor let-hero [ngForOf]=&quot;heroes&quot; let-i=&quot;index&quot; let-odd=&quot;odd&quot; [ngForTrackBy]=&quot;trackById&quot;&amp;gt;</p>
<template ngFor let-hero [ngForOf]=&quot;heroes&quot; let-i=&quot;index&quot; let-odd=&quot;odd&quot; [ngForTrackBy]=&quot;trackById&quot;>
<div [class.odd]=&quot;odd&quot;>({{i}}) {{hero.name}}</div>
</template>
</div>
<hr>
<h2 id=&quot;ngSwitch&quot;>NgSwitch</h2>
<div>Pick your favorite hero</div>
<p>
<ng-container *ngFor=&quot;let h of heroes&quot;>
<label>
<input type=&quot;radio&quot; name=&quot;heroes&quot; [(ngModel)]=&quot;hero&quot; [value]=&quot;h&quot;>{{h.name}}
</label>
</ng-container>
<label><input type=&quot;radio&quot; name=&quot;heroes&quot; (click)=&quot;hero = null&quot;>None of the above</label>
</p>
<h4>NgSwitch</h4>
<div [ngSwitch]=&quot;hero?.emotion&quot;>
<happy-hero *ngSwitchCase=&quot;'happy'&quot; [hero]=&quot;hero&quot;></happy-hero>
<sad-hero *ngSwitchCase=&quot;'sad'&quot; [hero]=&quot;hero&quot;></sad-hero>
<confused-hero *ngSwitchCase=&quot;'confused'&quot; [hero]=&quot;hero&quot;></confused-hero>
<unknown-hero *ngSwitchDefault [hero]=&quot;hero&quot;></unknown-hero>
</div>
<h4>NgSwitch with <i>template</i> attribute</h4>
<div [ngSwitch]=&quot;hero?.emotion&quot;>
<happy-hero template=&quot;ngSwitchCase 'happy'&quot; [hero]=&quot;hero&quot;></happy-hero>
<sad-hero template=&quot;ngSwitchCase 'sad'&quot; [hero]=&quot;hero&quot;></sad-hero>
<confused-hero template=&quot;ngSwitchCase 'confused'&quot; [hero]=&quot;hero&quot;></confused-hero>
<unknown-hero template=&quot;ngSwitchDefault&quot; [hero]=&quot;hero&quot;></unknown-hero>
</div>
<h4>NgSwitch with &amp;lt;template&amp;gt;</h4>
<div [ngSwitch]=&quot;hero?.emotion&quot;>
<template [ngSwitchCase]=&quot;'happy'&quot;>
<happy-hero [hero]=&quot;hero&quot;></happy-hero>
</template>
<template [ngSwitchCase]=&quot;'sad'&quot;>
<sad-hero [hero]=&quot;hero&quot;></sad-hero>
</template>
<template [ngSwitchCase]=&quot;'confused'&quot;>
<confused-hero [hero]=&quot;hero&quot;></confused-hero>
</template >
<template ngSwitchDefault>
<unknown-hero [hero]=&quot;hero&quot;></unknown-hero>
</template>
</div>
<hr>
<h2>&amp;lt;template&amp;gt;</h2>
<p>Hip!</p>
<template>
<p>Hip!</p>
</template>
<p>Hooray!</p>
<hr>
<h2 id=&quot;myUnless&quot;>UnlessDirective</h2>
<p>
The condition is currently
<span [ngClass]=&quot;{ a: !condition, b: condition, unless: true }&quot;>{{condition}}</span>.
<button
(click)=&quot;condition = !condition&quot;
[ngClass] = &quot;{ a: condition, b: !condition }&quot; >
Toggle condition to {{condition ? 'false' : 'true'}}
</button>
</p>
<p *myUnless=&quot;condition&quot; class=&quot;unless a&quot;>
(A) This paragraph is displayed because the condition is false.
</p>
<p *myUnless=&quot;!condition&quot; class=&quot;unless b&quot;>
(B) Although the condition is true,
this paragraph is displayed because myUnless is set to false.
</p>
<h4>UnlessDirective with template</h4>
<p *myUnless=&quot;condition&quot;>Show this sentence unless the condition is true.</p>
<p template=&quot;myUnless condition&quot; class=&quot;code unless&quot;>
(A) &amp;lt;p template=&quot;myUnless condition&quot; class=&quot;code unless&quot;&amp;gt;
</p>
<template [myUnless]=&quot;condition&quot;>
<p class=&quot;code unless&quot;>
(A) &amp;lt;template [myUnless]=&quot;condition&quot;&amp;gt;
</p>
</template>
<!--
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
-->"><input type="hidden" name="files[index.html]" value="<!DOCTYPE html>
<html>
<head>
<title>Angular Structural Directives</title>
<script>document.write('<base href=&quot;' + document.location + '&quot; />');</script>
<meta charset=&quot;UTF-8&quot;>
<meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;>
<link rel=&quot;stylesheet&quot; href=&quot;styles.css&quot;>
<!-- Polyfills -->
<script src=&quot;https://unpkg.com/core-js/client/shim.min.js&quot;></script>
<script src=&quot;https://unpkg.com/zone.js@0.7.4?main=browser&quot;></script>
<script src=&quot;https://unpkg.com/systemjs@0.19.39/dist/system.src.js&quot;></script>
<script src=&quot;https://cdn.rawgit.com/angular/angular.io/b3c65a9/public/docs/_examples/_boilerplate/systemjs.config.web.js&quot;></script>
<script>
System.import('main.js').catch(function(err){ console.error(err); });
</script>
</head>
<body>
<my-app>Loading...</my-apps>
</body>
</html>
<!--
Copyright 2016 Google Inc. All Rights Reserved.
Use of this source code is governed by an MIT-style license that
can be found in the LICENSE file at http://angular.io/license
-->"><input type="hidden" name="tags[0]" value="angular"><input type="hidden" name="tags[1]" value="example"><input type="hidden" name="tags[2]" value="structural"><input type="hidden" name="tags[3]" value="directives"><input type="hidden" name="tags[4]" value="template"><input type="hidden" name="tags[5]" value="ngIf"><input type="hidden" name="tags[6]" value="ngSwitch"><input type="hidden" name="tags[7]" value="ngFor"><input type="hidden" name="private" value="true"><input type="hidden" name="description" value="Angular Example - Structural directives"></form><script>document.getElementById("mainForm").submit();</script></body></html>