Created
August 23, 2016 10:56
-
-
Save ospaarmann/0f84cd713e0f184a6d9026b35f001915 to your computer and use it in GitHub Desktop.
Angular 2 nl2br pipe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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