586 lines
16 KiB
HTML
586 lines
16 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 { 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];
|
||
|
heroTraits = [ 'honest', 'brave', 'considerate' ];
|
||
|
|
||
|
// flags for the table
|
||
|
attrDirs = true;
|
||
|
strucDirs = true;
|
||
|
divNgIf = false;
|
||
|
|
||
|
showId = true;
|
||
|
showDefaultTraits = true;
|
||
|
showSad = true;
|
||
|
}
|
||
|
|
||
|
|
||
|
/*
|
||
|
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 { ContentComponent } from './content.component';
|
||
|
import { heroComponents } from './hero.components';
|
||
|
|
||
|
@NgModule({
|
||
|
imports: [ BrowserModule, FormsModule ],
|
||
|
declarations: [
|
||
|
AppComponent,
|
||
|
ContentComponent,
|
||
|
heroComponents
|
||
|
],
|
||
|
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/content.component.ts]" value="import { Component } from '@angular/core';
|
||
|
|
||
|
@Component({
|
||
|
selector: 'content-comp',
|
||
|
template:
|
||
|
`<div>
|
||
|
<ng-content></ng-content>
|
||
|
</div>`,
|
||
|
styles: [ `
|
||
|
div { border: medium dashed green; padding: 1em; width: 150px; text-align: center}
|
||
|
`]
|
||
|
})
|
||
|
export class ContentComponent { }
|
||
|
|
||
|
|
||
|
/*
|
||
|
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.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 && this.hero.name ?
|
||
|
`${this.hero.name} is strange and mysterious.` :
|
||
|
'Are you feeling indecisive?';
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export const heroComponents =
|
||
|
[ 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[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%;
|
||
|
}
|
||
|
|
||
|
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
|
||
|
}
|
||
|
|
||
|
td, th {
|
||
|
text-align: left;
|
||
|
vertical-align: top;
|
||
|
}
|
||
|
|
||
|
p span { color: red; font-size: 70%; }
|
||
|
|
||
|
|
||
|
/*
|
||
|
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>&lt;ng-container&gt;</h1>
|
||
|
|
||
|
<div *ngIf="hero">{{hero.name}}</div>
|
||
|
|
||
|
<hr>
|
||
|
|
||
|
<h3>&lt;ng-container&gt; and CSS</h3>
|
||
|
<p>Examples demonstrating issues with rigid CSS styles.</p>
|
||
|
|
||
|
<button (click)="hero = hero ? null : heroes[0]">Toggle hero</button>
|
||
|
|
||
|
<h4>#1 &lt;ng-container&gt; and &lt;p&gt;</h4>
|
||
|
<p>
|
||
|
I turned the corner
|
||
|
<ng-container *ngIf="hero">
|
||
|
and saw {{hero.name}}. I waved
|
||
|
</ng-container>
|
||
|
and continued on my way.
|
||
|
</p>
|
||
|
<p>
|
||
|
I turned the corner
|
||
|
<span *ngIf="hero">
|
||
|
and saw {{hero.name}}. I waved
|
||
|
</span>
|
||
|
and continued on my way.
|
||
|
</p>
|
||
|
|
||
|
<h4>#2 &lt;ng-container&gt; and &lt;p&gt;</h4>
|
||
|
|
||
|
<div *ngIf="hero">
|
||
|
<p>
|
||
|
{{hero.name}} is
|
||
|
<ng-container *ngFor="let trait of heroTraits; let first=first; let last=last">
|
||
|
<ng-container *ngIf="!first">,</ng-container>
|
||
|
<ng-container *ngIf="last">and</ng-container>
|
||
|
{{trait}}
|
||
|
</ng-container>.
|
||
|
</p>
|
||
|
|
||
|
<p>
|
||
|
{{hero.name}} is
|
||
|
<span *ngFor="let trait of heroTraits; let first=first; let last=last">
|
||
|
<span *ngIf="!first">,</span>
|
||
|
<span *ngIf="last">and</span>
|
||
|
{{trait}}
|
||
|
</span>.
|
||
|
</p>
|
||
|
|
||
|
<br>
|
||
|
|
||
|
<h4>#3 &lt;ng-container&gt; and &lt;p&gt;</h4>
|
||
|
<p>
|
||
|
<label><input type="checkbox" [checked]="showId" (change)="showId=!showId">Show ID</label>
|
||
|
</p>
|
||
|
|
||
|
<div>
|
||
|
The <code>hero.id</code> in the &lt;span&gt;
|
||
|
is caught by the <span>p-span</span> CSS:
|
||
|
<p>
|
||
|
<span *ngIf="showId">
|
||
|
Id: ({{hero.id}})
|
||
|
</span>
|
||
|
Name: {{hero.name}}
|
||
|
</p>
|
||
|
</div>
|
||
|
|
||
|
<div>
|
||
|
The <code>hero.id</code> in the &lt;ng-container&gt;
|
||
|
is unaffected by the <span>p-span</span> CSS:
|
||
|
<p>
|
||
|
<ng-container *ngIf="showId">
|
||
|
Id: ({{hero.id}})
|
||
|
</ng-container>
|
||
|
Name: {{hero.name}}
|
||
|
</p>
|
||
|
</div>
|
||
|
|
||
|
<div>
|
||
|
The <code>hero.id</code> in the &lt;template *ngIf&gt; disappears:
|
||
|
<p>
|
||
|
<template *ngIf="showId">
|
||
|
Id: ({{hero.id}})
|
||
|
</template>
|
||
|
Name: {{hero.name}}
|
||
|
</p>
|
||
|
</div>
|
||
|
|
||
|
<div>
|
||
|
The <code>hero.id</code> in the &lt;template [ngIf]&gt;
|
||
|
is unaffected by the <span>p-span</span> CSS:
|
||
|
<p>
|
||
|
<template [ngIf]="showId">
|
||
|
Id: ({{hero.id}})
|
||
|
</template>
|
||
|
Name: {{hero.name}}
|
||
|
</p>
|
||
|
</div>
|
||
|
|
||
|
</div>
|
||
|
|
||
|
<hr>
|
||
|
|
||
|
<h3>&lt;ng-container&gt; and layout-sensitive elements</h3>
|
||
|
<p>
|
||
|
Examples demonstrating issues with layout-sensitive elements
|
||
|
such as &lt;select&gt; and &lt;table&gt;.
|
||
|
</p>
|
||
|
|
||
|
<h4>#1 &lt;ng-container&gt; and &lt;options&gt;</h4>
|
||
|
|
||
|
<p><i>&lt;select&gt; with &lt;span&gt;</i></p>
|
||
|
<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>
|
||
|
|
||
|
<p><i>&lt;select&gt; with &lt;ng-container&gt;</i></p>
|
||
|
<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>
|
||
|
|
||
|
<br><br><br><br>
|
||
|
|
||
|
<h4>#2 &lt;ng-container&gt; and &lt;options&gt;</h4>
|
||
|
<p>
|
||
|
<label (change)="traitPicker.value=showDefaultTraits ? 'loyal' : heroTraits[0]">
|
||
|
<input type="checkbox"
|
||
|
[checked]="showDefaultTraits"
|
||
|
(change)="showDefaultTraits=!showDefaultTraits">Show default traits
|
||
|
</label>
|
||
|
</p>
|
||
|
|
||
|
<p>Options with &lt;ng-container&gt;</p>
|
||
|
|
||
|
<select #traitPicker>
|
||
|
<ng-container *ngIf="showDefaultTraits">
|
||
|
<!-- Default traits -->
|
||
|
<option value="loyal">LOYAL</option>
|
||
|
<option value="clean">CLEAN</option>
|
||
|
<option value="reverent">REVERENT</option>
|
||
|
</ng-container>
|
||
|
<option *ngFor="let trait of heroTraits" [value]="trait">
|
||
|
{{ trait | uppercase }}
|
||
|
</option>
|
||
|
</select>
|
||
|
|
||
|
|
||
|
<p>Options with &lt;span&gt;</p>
|
||
|
<select>
|
||
|
<!-- Default traits are always excluded because intermediate <span> is illegal -->
|
||
|
<span *ngIf="showDefaultTraits">
|
||
|
<option value="loyal">LOYAL</option>
|
||
|
<option value="clean">CLEAN</option>
|
||
|
<option value="reverent">REVERENT</option>
|
||
|
</span>
|
||
|
<option *ngFor="let trait of heroTraits" [value]="trait">
|
||
|
{{ trait | uppercase }}
|
||
|
</option>
|
||
|
</select>
|
||
|
|
||
|
<br>
|
||
|
|
||
|
<h4>&lt;ng-container&gt; and &lt;table&gt;</h4>
|
||
|
<p>
|
||
|
<label><input type="checkbox" checked (change)="attrDirs=!attrDirs">Attribute directives</label>
|
||
|
<label><input type="checkbox" checked (change)="strucDirs=!strucDirs">Structural directives</label>
|
||
|
<label><input type="checkbox" (change)="divNgIf=!divNgIf">div with *ngIf</label>
|
||
|
</p>
|
||
|
|
||
|
<table>
|
||
|
<tr>
|
||
|
<th width="20%">Directive</th>
|
||
|
<th width="10%">Type</th>
|
||
|
<th>Description</th>
|
||
|
</tr>
|
||
|
|
||
|
<tr *ngIf="attrDirs">
|
||
|
<td>NgClass</td><td>A</td><td>Add or remove multiple CSS classes.</td>
|
||
|
</tr>
|
||
|
|
||
|
<ng-container *ngIf="divNgIf">
|
||
|
<div *ngIf="strucDirs">
|
||
|
<tr>
|
||
|
<td>xxx</td><td>S</td><td>div with *ngIf formats crazy.</td>
|
||
|
</tr>
|
||
|
<tr>
|
||
|
<td>yyy</td><td>S</td><td>div with *ngIf formats crazy.</td>
|
||
|
</tr>
|
||
|
</div>
|
||
|
</ng-container>
|
||
|
|
||
|
<ng-container *ngIf="strucDirs">
|
||
|
<tr>
|
||
|
<td>NgFor</td><td>S</td><td>Repeat the template for each item in a list.</td>
|
||
|
</tr>
|
||
|
<tr>
|
||
|
<td>NgIf</td><td>S</td><td>Add or remove DOM elements.</td>
|
||
|
</tr>
|
||
|
</ng-container>
|
||
|
|
||
|
<ng-container *ngIf="attrDirs">
|
||
|
<tr>
|
||
|
<td>NgStyle</td><td>A</td><td>Add or remove multiple style attributes.</td>
|
||
|
</tr>
|
||
|
</ng-container>
|
||
|
|
||
|
<tr *ngIf="strucDirs">
|
||
|
<td>NgSwitch</td><td>S</td><td>Include in DOM if case matches the switch value.</td>
|
||
|
</tr>
|
||
|
|
||
|
</table>
|
||
|
|
||
|
<hr>
|
||
|
|
||
|
<h3>Do not confuse &lt;ng-container&gt; with &lt;ng-content&gt;</h3>
|
||
|
|
||
|
<p>&lt;ng-container&gt;Inside ng-container&lt;/ng-container&gt;</p>
|
||
|
<ng-container>Inside ng-container</ng-container>
|
||
|
|
||
|
<p>&lt;ng-content&gt;this is an Angular parse error&lt;/ng-content&gt;</p>
|
||
|
<!-- <ng-content>this is an Angular parse error</ng-content> -->
|
||
|
<div class="code">Template parse errors:<br>
|
||
|
&lt;ng-content&gt; element cannot have content.</div>
|
||
|
|
||
|
<p>Demo of &lt;/ng-content&gt;</p>
|
||
|
<content-comp>
|
||
|
Projected content
|
||
|
</content-comp>
|
||
|
|
||
|
|
||
|
<!--
|
||
|
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 &lt;ng-container&gt;</title>
|
||
|
<meta charset="UTF-8">
|
||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
|
<link rel="stylesheet" href="styles.css">
|
||
|
|
||
|
<!-- Polyfills -->
|
||
|
<script src="https://unpkg.com/core-js/client/shim.min.js"></script>
|
||
|
|
||
|
<script src="https://unpkg.com/zone.js@0.7.4?main=browser"></script>
|
||
|
<script src="https://unpkg.com/systemjs@0.19.39/dist/system.src.js"></script>
|
||
|
|
||
|
<script src="https://cdn.rawgit.com/angular/angular.io/b3c65a9/public/docs/_examples/_boilerplate/systemjs.config.web.js"></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="ngcontainer"><input type="hidden" name="tags[3]" value="structural"><input type="hidden" name="tags[4]" value="directives"><input type="hidden" name="private" value="true"><input type="hidden" name="description" value="Angular Example - <ng-container>"></form><script>document.getElementById("mainForm").submit();</script></body></html>
|