Angular1でフィルターと呼ばれていた機能が、Angular2ではパイプという名称で同様な提供されています。その使用方法を確認しました。
※Angular2の2.0.0版、TypeScriptを使って確認したものです。(デモはv4.4.3、v2.0.0で動作確認)
※目次をクリックすると目次の下部にコンテンツが表示されます。
主なビルトインタイプ
1)Dateパイプ
○構文
expression | date[:format]
例)
birthday = new Date(1988,3,15);
{{ birthday | date:”yMMdd” }}
→04/15/1988
2)Decimalパイプ
※Internationalization APIを使用しているので、ChromeとOperaのみ信頼性がある。
○構文
expression | number[:digitInfo]
・digitInfo
{minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}
minIntegerDigits:整数部の最小表示桁数
minFractionDigits:小数部の最小表示桁数
maxFractionDigits:小数部の最大表示桁数
例)
{{3.141592 | number:’3.2-4′}}→003.1416
{{3.1 | number:’3.2-4′}}→003.10
{{3.141592 | number:’1.1-1′}}→3.1
{{12345 | number}}→12,345
3)Currencyパイプ
※Internationalization APIを使用しているので、ChromeとOperaのみ信頼性がある。
○構文
expression | currency[:currencyCode[:symbolDisplay[:digitInfo]]]
・currencyCode
ISO 4217 currency code。日本の円は、’JPY’。
・symbolDisplay
“\”などのマークを表示するかbooleanで指定。
・digitInfo
Decimalパイプと同様。
例)
{{51259 | currency:’JPY’:false}}→JPY51,259
{{15315731 | currency:’JPY’:true}}→\15,315,731
4)JSONパイプ
・JSON.stringifyを使ってJSON文字列に変換
○構文
expression | json
5)Sliceパイプ
○構文
expression | slice:start[:end]
リストなどから”start”で指定したインデックスから”end”で指定したインデックスの前までのリストを抽出。”end”でマイナスを指定した場合は、最後尾から数えたインデックス。
例)
collection: string[] = [‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’];
<li *ngFor=”let i of collection | slice:1:3″>{{i}}</li>
→”1″、”2″が抽出
<li *ngFor=”let i of collection | slice:2:-3″>{{i}}</li>
→”2″から”6″(最後尾の10から3つである”7″の前まで)抽出
6)その他
・LowerCasePipe、UpperCasePipe、PercentPipeなどがある。
・Angular1で提供されていた、filter、orderByに相当するビルトインパイプはないようです。
○構文
expression | date[:format]
例)
birthday = new Date(1988,3,15);
{{ birthday | date:”yMMdd” }}
→04/15/1988
2)Decimalパイプ
※Internationalization APIを使用しているので、ChromeとOperaのみ信頼性がある。
○構文
expression | number[:digitInfo]
・digitInfo
{minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}
minIntegerDigits:整数部の最小表示桁数
minFractionDigits:小数部の最小表示桁数
maxFractionDigits:小数部の最大表示桁数
例)
{{3.141592 | number:’3.2-4′}}→003.1416
{{3.1 | number:’3.2-4′}}→003.10
{{3.141592 | number:’1.1-1′}}→3.1
{{12345 | number}}→12,345
3)Currencyパイプ
※Internationalization APIを使用しているので、ChromeとOperaのみ信頼性がある。
○構文
expression | currency[:currencyCode[:symbolDisplay[:digitInfo]]]
・currencyCode
ISO 4217 currency code。日本の円は、’JPY’。
・symbolDisplay
“\”などのマークを表示するかbooleanで指定。
・digitInfo
Decimalパイプと同様。
例)
{{51259 | currency:’JPY’:false}}→JPY51,259
{{15315731 | currency:’JPY’:true}}→\15,315,731
4)JSONパイプ
・JSON.stringifyを使ってJSON文字列に変換
○構文
expression | json
5)Sliceパイプ
○構文
expression | slice:start[:end]
リストなどから”start”で指定したインデックスから”end”で指定したインデックスの前までのリストを抽出。”end”でマイナスを指定した場合は、最後尾から数えたインデックス。
例)
collection: string[] = [‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’];
<li *ngFor=”let i of collection | slice:1:3″>{{i}}</li>
→”1″、”2″が抽出
<li *ngFor=”let i of collection | slice:2:-3″>{{i}}</li>
→”2″から”6″(最後尾の10から3つである”7″の前まで)抽出
6)その他
・LowerCasePipe、UpperCasePipe、PercentPipeなどがある。
・Angular1で提供されていた、filter、orderByに相当するビルトインパイプはないようです。
デモ
Angular : 4.4.3(2.0.0でも動作確認済み)
Bootstrap : 3.x
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';
import { FormsModule } from '@angular/forms';
@Component({
selector: 'my-app',
template: `
<strong>1)Dateパイプ</strong><br />
<p>new Date(1988,3,15) {{ birthday | date:"yMMdd" }}</p>
<strong>2)Decimalパイプ</strong><br />
<p>3.141592:(3.2-4): {{3.141592 | number:'3.2-4'}}</p>
<p>3.1 :(3.2-4): {{3.1 | number:'3.2-4'}}</p>
<p>3.141592:(1.1-1): {{3.141592 | number:'1.1-1'}}</p>
<p>12345 (number): {{12345 | number}}</p>
<strong>3)Currencyパイプ</strong><br />
<p>51259: {{51259 | currency:'JPY':false}}</p>
<p>15315731: {{15315731 | currency:'JPY':true}}</p>
<strong>4)Jsonパイプ</strong><br />
<p>Jsonパイプ未使用</p>
<pre>{{object}}</pre>
<p>Jsonパイプ使用</p>
<pre>{{object | json}}</pre>
<strong>5)Sliceパイプ</strong><br />
<input [(ngModel)]="start" type="text" placeholder="start">:
<input [(ngModel)]="end" type="text" placeholder="end">
<ul>
<li *ngFor="let i of collection | slice:start:end">{{i}}</li>
</ul>
`
})
export class AppComponent {
birthday = new Date(1988,3,15);
collection: string[] = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
object: Object = {foo: 'bar', nested: {numbers: [1, 2]}}
}
@NgModule({
imports: [
BrowserModule,
FormsModule
],
declarations: [
AppComponent
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
platformBrowserDynamic().bootstrapModule(AppModule);