[Angular -pipe] nl2br

[Angular -pipe] nl2br updated_at: 2024-02-16 13:53

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">

Table of contents 목차

평점을 남겨주세요
평점 : 5.0
총 투표수 : 1

질문 및 답글