๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป Frontend/Angular

[Angular] Custom Pipe ๋งŒ๋“ค์–ด ์ ์šฉํ•˜๊ธฐ (with. ngStyle)

by Fomagran ๐Ÿ’ป 2022. 6. 2.
728x90
๋ฐ˜์‘ํ˜•

์•ˆ๋…•ํ•˜์„ธ์š” Foma ์ž…๋‹ˆ๋‹ค!

 

์ €๋ฒˆ ์‹œ๊ฐ„์—๋Š” Angular ์ž์ฒด์— ๋‚ด์žฅ๋˜์–ด ์žˆ๋Š” Built-in Pipe์— ๋Œ€ํ•ด์„œ ์•Œ์•„ ๋ณด์•˜๋Š”๋ฐ์š”.

 

(์•„์ง ์•ˆ๋ณด์‹  ๋ถ„๋“ค์€ ์—ฌ๊ธฐ ์—์„œ ๋ด์ฃผ์„ธ์š”!)

 

์ด๋ฒˆ์—๋Š” ์ง์ ‘ Custom Pipe๋ฅผ ๋งŒ๋“ค์–ด์„œ ์ ์šฉํ•ด ๋ณด๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ •๋ฆฌํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.


Pipe ์ƒ์„ฑ

 

์•„๋ž˜ ๋ช…๋ น์–ด๋กœ Pipe๋ฅผ ๋งŒ๋“ค์–ด ์ค๋‹ˆ๋‹ค.

 

ng g p ํŒŒ์ดํ”„์ด๋ฆ„

//๋งŒ์•ฝ ํ…Œ์ŠคํŠธ ํŒŒ์ผ ์—†์ด ๋งŒ๋“ค๊ณ  ์‹ถ๋‹ค๋ฉด
ng g p --skip-tests=true ํŒŒ์ดํ”„์ด๋ฆ„

 

์ €๋Š” ํ‰๊ท  ๊ฐ’์— ๋”ฐ๋ผ์„œ ๋‹ค๋ฅธ ์ƒ‰์ด ๋ณด์ด๋„๋ก ํ•˜๋Š” AverageColorPipe๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

์•„๋ž˜์™€ ๊ฐ™์ด pipe.ts ํŒŒ์ผ์ด ๋งŒ๋“ค์–ด ์งˆ๊ฑฐ์—์š”.

 

@Pipe({})๋ฅผ ํ†ตํ•ด์„œ '|' ๊ธฐํ˜ธ ๋’ค์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” pipe ์ด๋ฆ„์„ ์ •ํ•ด ์ค๋‹ˆ๋‹ค.

 

๊ทธ๋ฆฌ๊ณค transform ๋ฉ”์„œ๋“œ ์•ˆ์— ์›ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ๊ฐ’์ด ๋‚˜์˜ค๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ์ค๋‹ˆ๋‹ค.

 

์ €๋Š” average๋ณด๋‹ค ์ž‘์œผ๋ฉด 'red' ๊ฐ™์œผ๋ฉด 'blue' ํฌ๋ฉด 'green'์ด ๋‚˜์˜ค๋„๋ก ํ•ด์คฌ์Šต๋‹ˆ๋‹ค.

 

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
  name: 'averageColor'
})
export class AverageColorPipe implements PipeTransform {
  transform(value: number, average: number): string {
    if (value < average) return 'red';
    if (value == average) return 'blue';
    return 'green';
  }
}

app.module.ts

 

Pipe๋ฅผ ๋ช…๋ น์–ด๋กœ ๋งŒ๋“ค๋ฉด app ๋ชจ๋“ˆ์— ์ž๋™์œผ๋กœ ์ถ”๊ฐ€ ๋˜์ง€๋งŒ ํ˜น์‹œ ์•ˆ๋˜์‹œ๋Š” ๋ถ„๋“ค์„ ์œ„ํ•ด ์ •๋ฆฌ ํ•ฉ๋‹ˆ๋‹ค.

 

์ƒ์„ฑํ•ด์ค€  pipe๋ฅผ import ํ•ด์ฃผ์‹œ๊ณ  @NgModule์˜ declarations์— ํ•ด๋‹น Pipe๋ฅผ ์ถ”๊ฐ€ํ•ด ์ค๋‹ˆ๋‹ค.

 

...
import { AverageColorPipe } from './average-color.pipe';

@NgModule({
  declarations: [AppComponent, AverageColorPipe],
 	...
})
export class AppModule {}

app.component.ts

 

component์—” ์ˆซ์ž๋“ค์ด ๋“ค์–ด์žˆ๋Š” ๋ฐฐ์—ด๊ณผ ํ•ด๋‹น ๋ฐฐ์—ด์˜ ์ด ํ•ฉ์˜ ํ‰๊ท ์„ ๊ตฌํ•ด ์ฃผ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
})
export class AppComponent implements OnInit {
  average: number = 0;
  numbers: number[] = [1, 2, 3, 4, 5, 6, 7];

  ngOnInit(): void {
    this.average =
      this.numbers.reduce((acc, curr) => {
        return acc + curr;
      }) / this.numbers.length;
  }
}

app.component.html

 

์ด์ œ ์•ž์—์„œ ์ƒ์„ฑํ•ด์ค€ averageColorํŒŒ์ดํ”„๋ฅผ ์ด์šฉํ•˜์—ฌ ์ˆซ์ž์˜ ๊ฐ’์— ๋”ฐ๋ผ ๊ธ€์ž ์ƒ‰์ด ๋ฐ”๋€Œ๋„๋ก ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์ˆซ์ž | averageColor:ํ‰๊ท ๊ฐ’์„ ๋„ฃ์–ด์ฃผ์‹œ๊ณ  ngStyle์„ ์ด์šฉํ•˜์—ฌ color ์ƒ‰์„ pipe์˜ return ๊ฐ’์ด ๋˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

 

<div *ngFor="let number of numbers">
  <h1 [ngStyle]="{ color: (number | averageColor: average) }">
    {{ number }}
  </h1>
</div>

 

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํ‰๊ท ๊ฐ’๋ณด๋‹ค ์ž‘์€ ์ˆซ์ž๋Š” ๋นจ๊ฐ„์ƒ‰์œผ๋กœ ๊ฐ™์€ ๊ฐ’์€ ํŒŒ๋ž€์ƒ‰์œผ๋กœ ํฐ ๊ฐ’์€ ์ดˆ๋ก์ƒ‰์œผ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€