Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow html in return of angular2 pipe?

Tags:

angular

pipe

I have a pipe that returns a html string, however the string outputs escaped presumably as a default for security. I'm sure there must be an option to allow html instead but cant find it when I search docs.

How can I tell the pipe to allow actual html to render?

like image 338
rgb Avatar asked Dec 29 '15 03:12

rgb


People also ask

What does pipe do in HTML?

“A pipe is a way to write display-value transformations that you can declare in your HTML. It takes in data as input and transforms it to a desired output”.

Can I use pipe in TS file Angular?

use date pipe in component ts files In laterst version of Angular i.e., from version 6 we can directly use angular pipes's public methods inside the components to format the values. For example we use date pipe format method formatMethod in component file by importing it from the @angular/common as shown below.

What does .pipe do in Angular?

Pipes let you combine multiple functions into a single function. The pipe() function takes as its arguments the functions you want to combine, and returns a new function that, when executed, runs the composed functions in sequence.


2 Answers

Use bindings to innerHTML or outerHTML to render already escaped html. For example <span [innerHTML]="someString | toHtmlPipe"></span>. See this plunk:

@Pipe({   name: 'wrapBold' }) class WrapBold {   transform(content) {     return `<b>${content}</b>`;   } }  @Component({   selector: 'my-app',   pipes: [WrapBold],   template: `     <div>       Hello <span [outerHTML]="content | wrapBold"></span>     </div>   ` }) export class App {   constructor() {     this.content = 'Angular2'   } } 
like image 179
alexpods Avatar answered Sep 20 '22 21:09

alexpods


I don't know if that is possible with a pipe. (It is, see @alexpods's answer.)

Have you considered converting your pipe into a component with an input property? I.e., whatever is feeding your pipe, make that an input property of the component. Put the HTML that the pipe generates into the component's template.

like image 21
Mark Rajcok Avatar answered Sep 19 '22 21:09

Mark Rajcok