Angular2のNgSwitchを使って、複数のHTMLテンプレートを選択して表示を切替える事ができます。
※Angular2の2.0.0版、TypeScriptを使って確認したものです。(デモはv4.4.3、v2.0.0で動作確認)
下記のようにボタンで選択した値(変数switch)の値に応じてHTMLテンプレートが選択されて表示されます。
何も選択されていない場合は、”ngSwitchDefault”で指定した内容が表示されます。
<input type="button" value="リストを表示" (click)="switch='list'">
<input type="button" value="テーブルを表示" (click)="switch='table'">
<div [ngSwitch]="switch">
<div *ngSwitchCase="'table'">
:
:テンプレートHTML
:
</div>
<div *ngSwitchCase="'list'">
:
:テンプレートHTML
:
</div>
<div *ngSwitchDefault>
上のボタンで選択したフォーマットでデータが表示されます。
</div>
</div>
デモ
Angular : 4.4.3(2.0.0でも動作確認済み)
Bootstrap : 3.x
Loading…
<html>
<head>
<title>Angular 2 Demo</title>
<script src="https://npmcdn.com/core-js/client/shim.min.js"></script>
<script src="https://unpkg.com/zone.js@0.7.4?main=browser"></script>
<script src="https://npmcdn.com/systemjs@0.19.39/dist/system.src.js"></script>
<script src="./js/systemjs.config.js"></script>
<script>
System.import('./ts/app.ts').catch(function(err){ console.error(err); }); </script>
<link rel="stylesheet" href="./css/bootstrap.min.css">
<link rel="stylesheet" href="./css/styles.css">
</head>
<body>
<div class="container">
<my-app>Loading...</my-app>
</div>
</body>
</html>
(systemjs.config.js)
(function (global) {
var ngVer = '@4.4.3';
System.config({
transpiler: 'ts',
typescriptOptions: {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": ["es2015", "dom"],
"noImplicitAny": true,
"suppressImplicitAnyIndexErrors": true,
"strict": true
},
meta: {
'typescript': {
"exports": "ts"
}
},
paths: {
'npm:': 'https://unpkg.com/'
},
map: {
'app': 'app',
'@angular/animations': 'npm:@angular/animations' + ngVer + '/bundles/animations.umd.js',
'@angular/animations/browser': 'npm:@angular/animations' + ngVer + '/bundles/animations-browser.umd.js',
'@angular/core': 'npm:@angular/core' + ngVer + '/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common' + ngVer + '/bundles/common.umd.js',
'@angular/common/http': 'npm:@angular/common' + ngVer + '/bundles/common-http.umd.js',
'@angular/compiler': 'npm:@angular/compiler' + ngVer + '/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser' + ngVer + '/bundles/platform-browser.umd.js',
'@angular/platform-browser/animations': 'npm:@angular/platform-browser' + ngVer + '/bundles/platform-browser-animations.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic' + ngVer + '/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http' + ngVer + '/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router' + ngVer + '/bundles/router.umd.js',
'@angular/router/upgrade': 'npm:@angular/router' + ngVer + '/bundles/router-upgrade.umd.js',
'@angular/forms': 'npm:@angular/forms' + ngVer + '/bundles/forms.umd.js',
'@angular/upgrade': 'npm:@angular/upgrade' + ngVer + '/bundles/upgrade.umd.js',
'@angular/upgrade/static': 'npm:@angular/upgrade' + ngVer + '/bundles/upgrade-static.umd.js',
// other libraries
'rxjs': 'npm:rxjs@5.0.1',
'tslib': 'npm:tslib/tslib.js',
'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js',
'ts': 'npm:plugin-typescript@5.2.7/lib/plugin.js',
'typescript': 'npm:typescript@2.3.2/lib/typescript.js',
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts',
meta: {
'./*.ts': {
loader: 'systemjs-angular-loader.js'
}
}
},
rxjs: {
defaultExtension: 'js'
}
}
});
})(this);
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { Component } from '@angular/core';
class Member {
name: string;
age: number;
address: string;
}
@Component({
selector: 'my-app',
template:`
<strong>NgSwitchを使って内部にあるHTMLの表示を切り替え</strong><br />
<input type="button" value="リストを表示" (click)="switch='list'">
<input type="button" value="テーブルを表示" (click)="switch='table'">
<br />
<div [ngSwitch]="switch">
<div *ngSwitchCase="'table'">
<table class="table">
<thead>
<tr><th>名前</th><th>年齢</th><th>住所</th></tr>
</thead>
<tbody>
<tr *ngFor="let member of members;">
<td>{{member.name}}</td>
<td>{{member.age}}</td>
<td>{{member.address}}</td>
</tr>
</tbody>
</table>
</div>
<div *ngSwitchCase="'list'">
<ul>
<li *ngFor="let member of members;">{{member.name}} ({{member.age}}) {{member.address}}</li>
</ul>
</div>
<div *ngSwitchDefault>
上のボタンで選択したフォーマットでデータが表示されます。
</div>
</div>
`
})
export class AppComponent {
public members = MEMBERS;
}
@NgModule({
imports: [
BrowserModule
],
declarations: [
AppComponent
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
platformBrowserDynamic().bootstrapModule(AppModule);
var MEMBERS: Member[] = [
{ "name": "相田", "age": 20 ,"address": "東京都品川区"},
・・・・
];