Angular
파이프
수색…
소개
파이프는 AngularJS의 필터와 매우 유사하므로 데이터를 지정된 형식으로 변환하는 데 도움이됩니다. 파이프 문자 | 각도로 파이프를 적용하는 데 사용됩니다.
맞춤 파이프
my.pipe.ts
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({name: 'myPipe'})
export class MyPipe implements PipeTransform {
transform(value:any, args?: any):string {
let transformedValue = value; // implement your transformation logic here
return transformedValue;
}
}
my.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-component',
template: `{{ value | myPipe }}`
})
export class MyComponent {
public value:any;
}
my.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { MyComponent } from './my.component';
import { MyPipe } from './my.pipe';
@NgModule({
imports: [
BrowserModule,
],
declarations: [
MyComponent,
MyPipe
],
})
export class MyModule { }
여러 개의 사용자 지정 파이프
서로 다른 파이프를 갖는 것은 매우 일반적인 경우입니다. 각 파이프는 서로 다른 작업을 수행합니다. 각 구성 요소에 각 파이프를 추가하는 것은 반복적 인 코드가 될 수 있습니다.
자주 사용하는 모든 파이프를 하나의 Module 에 묶어서 파이프를 필요로하는 모든 구성 요소에 새 모듈을 가져 오는 것이 가능합니다.
breaklines.ts
import { Pipe } from '@angular/core';
/**
* pipe to convert the \r\n into <br />
*/
@Pipe({ name: 'br' })
export class BreakLine {
transform(value: string): string {
return value == undefined ? value :
value.replace(new RegExp('\r\n', 'g'), '<br />')
.replace(new RegExp('\n', 'g'), '<br />');
}
}
대문자 .ts
import { Pipe } from '@angular/core';
/**
* pipe to uppercase a string
*/
@Pipe({ name: 'upper' })
export class Uppercase{
transform(value: string): string {
return value == undefined ? value : value.toUpperCase( );
}
}
pipes.module.ts
import { NgModule } from '@angular/core';
import { BreakLine } from './breakLine';
import { Uppercase} from './uppercase';
@NgModule({
declarations: [
BreakLine,
Uppercase
],
imports: [
],
exports: [
BreakLine,
Uppercase
]
,
})
export class PipesModule {}
my.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'my-component',
template: `{{ value | upper | br}}`
})
export class MyComponent {
public value: string;
}
my.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { MyComponent } from './my.component';
import { PipesModule} from './pipes.module';
@NgModule({
imports: [
BrowserModule,
PipesModule,
],
declarations: [
MyComponent,
],
})
Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow