[Angular -pipe] nl2br
nl2br
개행문자를 br 태그로 치환할 때 사용하는 pipe
- nl2br.pipe.ts
import { Pipe, PipeTransform, NgModule } from '@angular/core';
import { SafeHtml } from '@angular/platform-browser';
@Pipe({name: 'nl2br'})
export class Nl2brPipe implements PipeTransform {
constructor() {}
transform(str: string): SafeHtml {
if (typeof (str) === 'undefined') { return ''; }
if (!str) { return ''; }
const replaced = str.replace(/\n/g, '<br/>');
return replaced;
}
}
@NgModule({
declarations: [ Nl2brPipe ],
exports: [ Nl2brPipe ],
})
export class Nl2brPipeModule { }
<div class="text-box" [innerHTML]="item.content | nl2br">