CHAPTER 7. Pipes
Pied piper
๋๋ก๋ ๊ธฐ์กด ๋ฐ์ดํฐ๊ฐ ๋ทฐ์ ํ์ ํ ๋ฐ์ดํฐ๊ฐ ์๋ ๊ฒฝ์ฐ๊ฐ ์๋ค. ์ฐ๋ฆฌ๋ ์ข ์ข ํํฐ๋ฅผ ๋ณํํ๊ณ , ํํฐ๋ฅผ ๊ฑธ๊ณ , ํํฐ๋ฅผ ์ ํํ๋ ๋ฑ์ ์์ ์ ์ํ๋ค. AngularJS 1.x์๋ 'ํํฐ'๋ผ๊ณ ํ๋ ์ด๋ฆ์ด ๋งค์ฐ ์ ์ฉํ๊ฒ ์ฌ์ฉ๋์๋ค.
ํ์ดํ๋ HTML ๋๋ ์์ฉ ํ๋ก๊ทธ๋จ ์ฝ๋์์ ์ฌ์ฉํ ์ ์๋ค. ์๋ฅผ ๋ค๊ณ ์ฐ๋ฆฌ๊ฐ ๊ทธ๊ฒ์ ์ด๋ป๊ฒ ์ฌ์ฉํ ์ ์๋์ง ๋ณด์.
Json
JsonPipe๋ ๊ฐ๋ฐ ์์ฉ ํ๋ก๊ทธ๋จ์์๋ ๋ณ๋ก ์ ์ฉํ์ง ์์ง๋ง ์์ฉ ํ๋ก๊ทธ๋จ์ ๋๋ฒ๊น ํ ๋๋ ๋งค์ฐ ์ ์ฉํ๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก ์ด ํ์ดํ๋ JSON.stringify ()๋ฅผ ๋ฐ์ดํฐ์ ์ ์ฉํ๋ค. ๊ตฌ์ฑ ์์์ ์ผ๋ถ ๋ฐ์ดํฐ๊ฐ ์๋ ๊ฒฝ์ฐ pony ๋ฐฐ์ด์ ์๋ก ๋ค์๋ฉด, ๋ด๋ถ์ ๋ฌด์์ด ์๋์ง ์ ์ํ๊ฒ ํ์ธํ๋ ค๋ ๊ฒฝ์ฐ, ๋น์ ์ ๋ค์๊ณผ ๊ฐ์ ๊ฒ์ ์๋ ํ ์ ์๋ค.
<p>{% raw %}{{ ponies }}{% endraw %}</p>
์ด์ด ์ข์ผ๋ฉด [object object]๊ฐ ํ์ ๋ ๊ฒ์ด๋ค. ํ์ง๋ง JsonPipe๋ ์ฐ๋ฆฌ๋ฅผ ๊ตฌํ๊ธฐ ์ํด ์ฌ๊ธฐ์ ์๊ณ ์ด๋ค ์ ์ผ๋ก๋ HTML์์ ์ฌ์ฉํ ์ ์๋ค.
<p>{% raw %}{{ ponies | json }}{% endraw %}</p>
๊ทธ๋ฆฌ๊ณ ๊ทธ๊ฒ์ ๊ฐ์ฒด์ JSON ํํ์ ํ์ํ๋ค.
<p>[ { "name": "Rainbow Dash" }, { "name": "Pinkie Pie" } ]</p>
ํ์ดํ๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด ๋ฐ์ดํฐ ๋ค์์ ํ์ดํ (|) ๋ฌธ์๋ฅผ ์ถ๊ฐ ํ ๋ค์ ์ฌ์ฉํ ํ์ดํ์ ์ด๋ฆ์ ์ถ๊ฐํด์ผ ํ๋ค. ํํ์์ด ํ๊ฐ๋๊ณ ๊ฒฐ๊ณผ๊ฐ ํ์ดํ๋ฅผ ํต๊ณผํ ๊ฒ์ด๋ค. ์ฌ๋ฌ ํ์ดํ๋ฅผ ์ฐจ๋ก๋ก ์ฐ๊ฒฐํ ์ ์๋ค.
<p>{% raw %}{{ ponies | slice:0:2 | json }}{% endraw %}</p>
์ฐ๋ฆฌ๋ ์ฌ๋ผ์ด์ค ํ์ดํ๋ก ๋์๊ฐ ๊ฒ์ด์ง๋ง ์ฌ๋ผ์ด์ค ํ์ดํ๋ฅผ ์ฐ๊ฒฐ ํ ๋ค์ json์ ์ฐ๊ฒฐํ๋ ๊ฒ์ ๋ณผ ์ ์๋ค. "
"๋ณด๊ฐ ํํ์์ด๋ ์์ฑ ํํ์์์๋ ์ฌ์ฉํ ์ ์์ง๋ง ์ด๋ฒคํธ ๋ฌธ์์๋ ์ฌ์ฉํ ์ ์๋ค.
<p [textContent]="ponies | json"></p>
์์กด์ฑ ์ฝ์ ์ ํตํด ์ฝ๋์์ ์ฌ์ฉํ ์๋ ์๋ค.
import { Component } from '@angular/core';
// you need to import the pipe you want to use
import { JsonPipe } from '@angular/common';
@Component({
selector: 'ns-ponies',
template: `<p>{% raw %}{{poniesAsJson}}{% endraw %}</p>`
})
export class PoniesComponent {
ponies: Array<any> = [{ name: 'Rainbow Dash' }, { name: 'Pinkie Pie' }];
poniesAsJson: string;
// inject the Pipe you want
constructor(jsonPipe: JsonPipe) {
// and then call the transform method on it
this.poniesAsJson = jsonPipe.transform(this.ponies);
}
}
๊ทธ๋ฌ๋ ์กฐ์ฌ ํด์ผํ๋ค : ํ์ดํ๋ @NgModule (๋๋ @Component)์ provider์ ์ถ๊ฐ๋์ด์ผ ํ๋ค. ์ด๊ฒ์ ๋ชจ๋ ํ์ดํ์์ ๋์ผํ๋ฏ๋ก ๋ณด๊ฐ๋ฒ์ ์ฌ์ฉํ์ฌ HTML ์์ ๋ฅผ ๋ณด๋๋ก ํ์.
<p>{% raw %}{{ ponies | slice:0:2 | json }}{% endraw %}</p>
์ด ์์ ๋ ์กฐ๋๋ง ๋ชฉ๋ก์ ์ฒ์ ๋ ์์๋ฅผ ํ์ํ๋ค. slice๋ ๋ฐฐ์ด๊ณผ ๋ฌธ์์ด๋ก ์๋ํ๋ฏ๋ก ๋ฌธ์์ด์ ์๋ฅผ ์๋ ์๋ค.
<p>{% raw %}{{ 'Ninja Squad' | slice:0:5 }}{% endraw %}</p>
'Ninja'๋ง ํ์๋ ๊ฒ์ด๋ค. ์ฌ๋ผ์ด์ค ํ์ดํ์ ํ๋์ ์ธ๋ฑ์ค n๋ง์ ์ค ์ ์์ผ๋ฉฐ n์์ ๋๊น์ง ์์๋ฅผ ๊ฐ์ ธ์ฌ๊ฒ์ด๋ค.
<p>{% raw %}{{ 'Ninja Squad' | slice:3 }}{% endraw %}</p>
<!-- will display 'ja Squad' -->
์์๋ฅผ ์ง์ ํ๋ฉด ๋ค์์ ๋ถํฐ ์๋ฅผ ๊ฒ์ด๋ค.
<p>{% raw %}{{ 'Ninja Squad' | slice:-5 }}{% endraw %}</p>
<!-- will display 'Squad' -->
์ฐ๋ฆฌ๊ฐ ๋ณด์ ๋ฏ์ด, ํ์ดํ์๊ฒ ์ข ๋ฃ ์ธ๋ฑ์ค๋ฅผ ์ค ์ ์๋ค :์ด ์ธ๋ฑ์ค๊น์ง ์์๋ฅผ ๊ฐ์ ธ์ต๋๋ค. ์ด ์ธ๋ฑ์ค๊ฐ ์์์ด๋ฉด ์ธ๋ฑ์ค๊น์ง ์์๋ฅผ ์ทจํ์ง๋ง ๋์์๋ถํฐ ์์ํ๋ค.
<p>{% raw %}{{ 'Ninja Squad' | slice:2:-2 }}{% endraw %}</p>
<!-- will display 'nja Squ' -->
์์์ ํํ์์์ slice๋ฅผ ์ฌ์ฉํ ์ ์์ผ๋ฏ๋ก NgFor์์๋ ์ฌ์ฉํ ์ ์๋ค.
import { Component } from '@angular/core';
@Component({
selector: 'ns-ponies',
template: `<div *ngFor="let pony of ponies | slice:0:2">{% raw %}{{pony.name}}{% endraw %}</div>`
})
export class PoniesComponent {
ponies: Array<any> = [
{ name: 'Rainbow Dash' },
{ name: 'Pinkie Pie' },
{ name: 'Fluttershy' }
];
}
์ด ๊ตฌ์ฑ ์์๋ ์ปฌ๋ ์ ์ ์ฌ๋ผ์ด์ค ํ์ดํ๋ฅผ ์ ์ฉ ํ์ผ๋ฏ๋ก ์ฒซ ๋ฒ์งธ ๋ ์กฐ๋๋ง์ ๋ํด ๋ ๊ฐ์ div ์์๋ฅผ ๋ง๋ ๋ค. ํ์ดํ ์ธ์๋ ๋ฌผ๋ก ๋์ ๊ฐ์ด ๋ ์ ์์ต๋๋ค.
import { Component } from '@angular/core';
@Component({
selector: 'ns-ponies',
template: `<div *ngFor="let pony of ponies | slice:0:size">{% raw %}{{pony.name}}{% endraw %}</div>`
})
export class PoniesComponent {
size: number = 2;
ponies: Array<any> = [
{ name: 'Rainbow Dash' },
{ name: 'Pinkie Pie' },
{ name: 'Fluttershy' }
];
}
์ด ๊ธฐ๋ฅ์ ์ฌ์ฉํ์ฌ ์ฌ์ฉ์๊ฐ ๋ณด๊ณ ์ถ์ ์์์ ์๋ฅผ ์ ํํ ์ ์๋ ๋์ ๋์คํ๋ ์ด๋ฅผ ๋ง๋ค ์ ์๋ค.
์ด ํ์ดํ์ ์ด๋ฆ์ด ์ถฉ๋ถํ ๋ช ํ ํด์ง๋ฉด์์ด ํ์ดํ๋ ๋ฌธ์์ด์ ๋๋ฌธ์๋ก ๋ณํ ํ๋ค.
<p>{% raw %}{{ 'Ninja Squad' | uppercase }}{% endraw %}</p>
<!-- will display 'NINJA SQUAD' -->
์ด ํ์ดํ์ ์ด๋ฆ์ด ์ถฉ๋ถํ ๋ช ํ ํด์ง๋ฉด์์ด ํ์ดํ๋ ๋ฌธ์์ด์ ์๋ฌธ์๋ก ๋ณํ ํ๋ค.
<p>{% raw %}{{ 'Ninja Squad' | lowercase }}{% endraw %}</p>
<!-- will display 'ninja squad' -->
์ด ํ์ดํ๋ ์ซ์์ ์์์ ์ง์ ํ ์ ์๋ค. {integerDigits} {minFractionDigits} - {maxFractionDigits} ํ์์ ๋ฌธ์์ด ํ๋๊ฐ ํ์ํ์ง๋ง ๋ชจ๋ ๋ถ๋ถ์ ์ ํ ์ฌํญ ์ด๋ค. ๊ฐ ๋ถ๋ถ์ ๋ค์์ ๋ํ๋ธ๋ค. โข ์ ์ ๋ถ๋ถ์ ์ํ๋ ์์ ๊ฐ์ โข ์์ ๋ถ๋ถ์์ ์ํ๋ ์ซ์์ ์ โข ์์ ๋ถ๋ถ์์ ์ํ๋ ์ซ์์ ์ ํ์ดํ๊ฐ ์๋ ๊ฒ๋ถํฐ ์์ํ๋ ๋ช ๊ฐ์ง ์์ด๋ค.
<p>{% raw %}{{ 12345 }}{% endraw %}</p>
<!-- will display '12345' -->
์ซ์ ํ์ดํ๋ฅผ ์ฌ์ฉํ๋ฉด ์ซ์๊ฐ ํ์ํ์ง ์์ ๊ฒฝ์ฐ์๋ ์ ์ ๋ถ๋ถ์ด ๊ทธ๋ฃนํ ๋๋ค.
<p>{% raw %}{{ 12345 | number }}{% endraw %}</p>
<!-- will display '12,345' -->
integerDigits ๋งค๊ฐ ๋ณ์๋ ํ์ํ ๊ฒฝ์ฐ ์ ์ ๋ถ๋ถ์ 0์ผ๋ก ์ผ์ชฝ์ ์ฑ์ ์ค๋ค.
<p>{% raw %}{{ 12345 | number:'6.' }}{% endraw %}</p>
<!-- will display '012,345' -->
minFractionDigits๋ ์์ ๋ถ๋ถ์ ์ต์ ํฌ๊ธฐ์ด๋ฏ๋ก ๋๋ฌ ํ ๋๊น์ง ์ค๋ฅธ์ชฝ์ 0์ ์ฑ์ ์ค๋ค.
<p>{% raw %}{{ 12345 | number:'.2' }}{% endraw %}</p>
<!-- will display '12,345.00' -->
maxFractionDigits๋ ์์ ๋ถ๋ถ์ ์ต๋ ํฌ๊ธฐ์ด๋ค. ๊ทธ๊ฒ์ ์ํ๋ค๋ฉด minFractionDigits๋ฅผ 0์ผ๋ก ์ง์ ํด์ผ ํ๋ค. ์ซ์๊ฐ ๊ทธ ๋ณด๋ค ์์ ์ด์์ด๋ฉด ๋ฐ์ฌ๋ฆผ๋๋ค.
<p>{% raw %}{{ 12345.13 | number:'.1-1' }}{% endraw %}</p>
<!-- will display '12,345.1' -->
<p>{% raw %}{{ 12345.16 | number:'.1-1' }}{% endraw %}</p>
<!-- will display '12,345.2' -->
ํ์ดํ (๋ฐฑ๋ถ์จ๊ณผ ํตํ๋ ๋ฌผ๋ก )๋ ๋ธ๋ผ์ฐ์ ์ ๊ตญ์ ํ API๋ฅผ ์ฌ์ฉํ๋ฉฐ ์ด API๋ ํ์ฌ ๋ชจ๋ ๋ธ๋ผ์ฐ์ ์์ ์ฌ์ฉํ ์ ์๋ค. ์ด๋ ์๋ ค์ง ๋ฌธ์ ์ด๋ฉฐ ์ผ๋ถ ๋ธ๋ผ์ฐ์ (Safari / iOS ๋ฑ)์์์ด API์ ๋ํด polyfill์ ์ฌ์ฉํ๋๋ก ํด์ผ ํ๋ค.
์ซ์์ ๋์ผํ ์์น์ ๋ฐ๋ผ ๋ฐฑ๋ถ์จ์ ๋ฐฑ๋ถ์จ์ ํ์ ํ ์ ์๋ค.
<p>{% raw %}{{ 0.8 | percent }}{% endraw %}</p>
<!-- will display '80%' -->
<p>{% raw %}{{ 0.8 | percent:'.3' }}{% endraw %}</p>
<!-- will display '80.000%' -->
์์ํ ์ ์๋ฏ์ด ํ์ดํ๋ ์ํ๋ ํตํ๋ก ๊ธ์ก์ ํ์ํ ํ ์ ์๋ค. ์ ์ด๋ ํ๋์ ๋งค๊ฐ ๋ณ์๋ฅผ ์ง์ ํด์ผ ํ๋ค.
โข ํตํ๋ฅผ ๋ํ๋ด๋ ISO ๋ฌธ์์ด ( 'EUR', 'USD'...) โข ์ ํ์ ์ผ๋ก ๊ธฐํธ ( 'โฌ', '$') ๋๋ ISO ๋ฌธ์์ด์ ์ฌ์ฉํ ์ง ์ฌ๋ถ๋ฅผ ๋ํ๋ด๋ ๋ถ์ธ ํ๋๊ทธ. ๊ธฐ๋ณธ์ ์ผ๋ก, ํ๋๊ทธ๋ false์ด๋ฉฐ ์ฌ๋ณผ์ ์ฌ์ฉ๋์ง ์๋๋ค. โข ์ ํ์ ์ผ๋ก number์ ๋์ผํ ๊ตฌ๋ฌธ์ ์ฌ์ฉํ์ฌ ๊ธ์ก์ ํ์ํํ๋ ๋ฌธ์์ด
<p>{% raw %}{{ 10.6 | currency:'EUR' }}{% endraw %}</p>
<!-- will display 'EUR10.60' -->
<p>{% raw %}{{ 10.6 | currency:'USD':true }}{% endraw %}</p>
<!-- will display '$10.60' -->
<p>{% raw %}{{ 10.6 | currency:'USD':true:'.3' }}{% endraw %}</p>
<!-- will display '$10.600' -->
๋ ์ง ํ์ดํ๋ ๋ ์ง ๊ฐ์ ์ํ๋ ํ์์ ๋ฌธ์์ด๋ก ํฌ๋งท์ด๋ค. ๋ ์ง๋ Date ๊ฐ์ฒด ๋๋ ๋ฐ๋ฆฌ ์ด๋ฅผ ๋ํ๋ธ๋ค. ์ง์ ๋ ํ์์ 'dd / MM / yyyy', 'MM-yy'๋๋ 'short', 'longDate'๋ฑ๊ณผ ๊ฐ์ด ๋ฏธ๋ฆฌ ์ ์ ๋ ๊ธฐํธ ์ด๋ฆ ์ค ํ๋ ์ผ ์ ์๋ค.
<p>{% raw %}{{ birthday | date:'dd/MM/yyyy' }}{% endraw %}</p>
<!-- will display '16/07/1986' -->
<p>{% raw %}{{ birthday | date:'longDate' }}{% endraw %}</p>
<!-- will display 'July 16, 1986' -->
๋ฌผ๋ก ๋ ์ง์ ์๊ฐ ๋ถ๋ถ์ ํ์ ํ ์๋ ์๋ค.
<p>{% raw %}{{ birthday | date:'HH:mm' }}{% endraw %}</p>
<!-- will display '15:30' -->
<p>{% raw %}{{ birthday | date:'shortTime' }}{% endraw %}</p>
<!-- will display '3:30 PM' -->
๋น๋๊ธฐ ํ์ดํ๋ฅผ ์ฌ์ฉํ๋ฉด ๋น๋๊ธฐ ์ ์ผ๋ก ๊ฐ์ ธ์จ ๋ฐ์ดํฐ๋ฅผ ํ์ ํ ์ ์๋ค. ๋ด๋ถ์ ์ผ๋ก ๋น๋๊ธฐ ๋ฐ์ดํฐ๊ฐ Promise ๋๋ Observable์์ ์จ ๊ฒ์ธ์ง ์ฌ๋ถ์ ๋ฐ๋ผ PromisePipe ๋๋ ObservablePipe๋ฅผ ์ฌ์ฉํ๋ค. Promise๊ฐ ๋ฌด์์ธ์ง ์ด์ ๋ ์๊ณ ์์ด์ผ ํ๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด ES6 ์ฅ์ผ๋ก ๋์๊ฐ์ Observable๋ฅผ ํ์ธํ๋๋ก ํ๋ผ.
๋น๋๊ธฐ ํ์ดํ๋ ๋ฐ์ดํฐ๊ฐ ์ต์ข ์ ์ผ๋ก ์ฌ์ฉ ๊ฐ๋ฅํ ๋๊น์ง (์ฆ, ์ฝ์์ด ํ์ ๋ ๋๊น์ง) ๋น ๋ฌธ์์ด์ ๋ฐํํ๋ค. ์ผ๋จ ํด๊ฒฐ๋๋ฉด ํด๊ฒฐ ๋ ๊ฐ์ด ๋ฐํ๋๋ค. ๋ ์ค์ํ ๊ฒ์ ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ ์์๊ฒ ๋๋ฉด ๋ณ๊ฒฝ ๊ฐ์ง ๊ฒ์ฌ๋ฅผ ํธ๋ฆฌ๊ฑฐํ๊ฒ ๋๋ค.
import { Component } from '@angular/core';
@Component({
selector: 'ns-greeting',
template: `<div>{% raw %}{{ asyncGreeting | async }}{% endraw %}</div>`
})
export class GreetingComponent {
asyncGreeting = new Promise(resolve => {
// after 1 second, the promise will resolve
window.setTimeout(() => resolve('hello'), 1000);
});
}
๋น๋๊ธฐ ํ์ดํ๊ฐ asyncGreeting ๋ณ์์ ์ ์ฉ๋ ๊ฒ์ ๋ณผ ์ ์๋ค. 1์ด ํ์ ๊ฐ์ด ๋ฐ์ธ๋ฉ ๋๋ฉด ํธ๋ฆฌ๊ฑฐ๋ฅผ ํ ๊ฒ์ด๋ค.
<div>hello</div>
๋์ฑ ํฅ๋ฏธ๋ก์ด ๊ฒ์ ์์ค๊ฐ Observable ์ธ ๊ฒฝ์ฐ ์ฌ์ฉ์๊ฐ ๋ค๋ฅธ ๊ตฌ์ฑ ์์๋ฅผ ํ์ ํ ๋์ ๊ฐ์ด ๊ตฌ์ฑ ์์๊ฐ ํ๊ดด ๋ ๋ ํ์ดํ๊ฐ ๊ตฌ๋ ์ทจ์ ๋ถ๋ถ ์์ฒด๋ฅผ ์ฒ๋ฆฌํ๋ค๋ ๊ฒ์ด๋ค.
๋ฌผ๋ก ํ์ดํ๋ฅผ ์ง์ ๋ง๋ค ์๋ ์๊ณ ๋๋ก๋ ๋งค์ฐ ์ ์ฉํ๋ค. AngularJS 1.x์์๋ ๋ง์ถค ํํฐ๋ฅผ ์์ฃผ ์ฌ์ฉํ์๋ค. ์๋ฅผ ๋ค์ด, ์ฐ๋ฆฌ๋ ์ฐ๋ฆฌ์ ์ ํ ๋ฆฌ์ผ์ด์ ์ ๋ช ๊ฐ์ง (3 ์ผ ์ agoor 12์ด ๋ฑ) ์ฌ์ฉ์๊ฐ ํ ์์ ๊ฒฝ๊ณผ ์ผ๋ง๋ ๋ง์ ์๊ฐ์ ํ์ ํ๋๋ฅผ ๋ง๋ค์๋ค. Angular 2์์ ์ฐ๋ฆฌ๊ฐ ์ด๋ป๊ฒ ํ ๊ฒ์ธ์ง ๋ณด์.
๋จผ์ ์๋ก์ด ํด๋์ค๋ฅผ ๋ง๋ค์ด์ผ ํ๊ณ ๊ทธ๊ฒ์ PipeTransform ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํด์ผ ํ๋ค. ์ด ์ธํฐํ์ด์ค๋ ๋ฌด๊ฑฐ์ด ๋ฌผ๊ฑด์ ๋ค์ฌ๋ค ๋ณด๋ transform () ๋ฉ์๋๋ฅผ ํ์๋ก ํ๋ค.
import { PipeTransform, Pipe } from '@angular/core';
export class FromNowPipe implements PipeTransform {
transform(value, args) {
// do something here
}
}
Now ํจ์์ Moment.js๋ฅผ ์ฌ์ฉํ์ฌ ๋ ์ง ์ดํ ์ผ๋ง๋ ๋ง์ ์๊ฐ์ด ๊ฒฝ๊ณผํ๋์ง ํ์ํ๋ค. ์ํ๋ ๊ฒฝ์ฐ NPM์ ์ฌ์ฉํ์ฌ Moment.js๋ฅผ ์ค์นํ ์ ์๋ค.
npm install moment
SystemJS ๊ตฌ์ฑ์ ์ถ๊ฐํ์ฌ๋ผ.
map: {
'@angular': 'node_modules/@angular',
'rxjs': 'node_modules/rxjs',
'moment': 'node_modules/moment/moment'
}
TypeScript๋ฅผ ์ฌ์ฉํ ๋ ์ธํฐํ์ด์ค๋ ํ์ํ๋ค.
typings install --save --ambient moment-node
import { PipeTransform, Pipe } from '@angular/core';
import * as moment from 'moment';
export class FromNowPipe implements PipeTransform {
transform(value, args) {
return moment(value).fromNow();
}
}
์ด์ ์ฐ๋ฆฌ๋ ์ฑ์ ํ์ดํ๋ฅผ ๋ฑ๋กํด์ผ ํ๋ค. ์ด๋ฅผ ์ํด ์ฐ๋ฆฌ๊ฐ ์ฌ์ฉํ ์์๋ ํน๋ณํ ๋ฐ์ฝ๋ ์ดํฐ๊ฐ ์๋ค : @Pipe
import { PipeTransform, Pipe } from '@angular/core';
import * as moment from 'moment';
@Pipe({ name: 'fromNow' })
export class FromNowPipe implements PipeTransform {
transform(value, args) {
return moment(value).fromNow();
}
}
์ ํํ ์ด๋ฆ์ ํ ํ๋ฆฟ์์ ํ์ดํ๋ฅผ ์ฌ์ฉํ ์์๋ ์ด๋ฆ์ด ๋๋ค. ํ ํ๋ฆฟ์์ ํ์ดํ๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด ๋ง์ง๋ง์ผ๋กํด์ผ ํ ์ผ์ @NgModule์ ์ ์ธ์ ํ์ดํ๋ฅผ ์ถ๊ฐํ๋ ๊ฒ์ด๋ค.
@NgModule({
imports: [BrowserModule],
declarations: [PonyRacerAppComponent, RacesComponent, FromNowPipe],
bootstrap: [PonyRacerAppComponent]
})
export class AppModule {
}
slice
๋ชฉ๋ก์ ์ผ๋ถ๋ง ํ์ํ๋ ค๋ฉด slice๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค. ์๋ฐ ์คํฌ๋ฆฝํธ์์ ์ฌ๋ผ์ด์ค ๋ฉ์๋์ฒ๋ผ ์๋ํ๋ฉฐ ์์ ์ธ๋ฑ์ค์ ์ ํ์ ์ผ๋ก ์ข ๋ฃ ์ธ๋ฑ์ค๋ผ๋ ๋ ๊ฐ์ ์ธ์๋ฅผ ์ทจํ๋ค.
์ธ์๋ฅผ ํ์ดํ์ ์ ๋ฌํ๋ ค๋ฉด ์ฝ๋ก :์ ์ถ๊ฐํ๊ณ ์ฒซ ๋ฒ์งธ ์ธ์ ๋ค์์ ์ฝ๋ก (:) ๋ฐ ๋ ๋ฒ์งธ ์ธ์ ๋ฑ์ ์ถ๊ฐํด์ผ ํ๋ค.
Reference URL
Last updated
Was this helpful?