Skip to content

Instantly share code, notes, and snippets.

@ospaarmann
Created August 23, 2016 10:56
Show Gist options
  • Save ospaarmann/0f84cd713e0f184a6d9026b35f001915 to your computer and use it in GitHub Desktop.
Save ospaarmann/0f84cd713e0f184a6d9026b35f001915 to your computer and use it in GitHub Desktop.
Angular 2 nl2br pipe
import { Component} from '@angular/core';
import { Nl2BrPipe } from './nl2br.pipe';
@Component({
moduleId: module.id,
selector: 'my-component',
template: `
<div [innerHtml]="content | nl2br"></div>
`
pipes: [Nl2BrPipe]
})
export class MyComponent {
private content:string;
constructor() {
this.content = "Hello \n\n this is in a new line";
}
}
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({name: 'nl2br'})
export class Nl2BrPipe implements PipeTransform {
transform(value: string, args: string[]): any {
if (!value) return value;
let breakTag = '<br>';
return (value + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1'+ breakTag +'$2');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment