How to translate HTML string to real HTML element by ng-for in Angular 2?
Asked Answered
P

5

22

As I know, in angular 1.x I can use $sce service to meet my requirment like this

myApp.filter('trustAsHTML', ['$sce', function($sce){
  return function(text) {
    return $sce.trustAsHtml(text);
  };
}]);

and in html file use like this

{{ htmlString || trustAsHTML }}

Does there has a service like $sce or some pipe or any method can be competent to do that in angularjs 2 version?

Plaint answered 2/11, 2015 at 7:4 Comment(0)
A
30

In angular2 there's no ng-include, trustAsHtml, ng-bind-html nor similar, so your best option is to bind to innerHtml. Obviously this let you open to all kind of attacks, so it's up to you to parse/escape the content and for that you can use pipes.

@Pipe({name: 'escapeHtml', pure: false})
class EscapeHtmlPipe implements PipeTransform {
   transform(value: any, args: any[] = []) {
       // Escape 'value' and return it
   }
}

@Component({
    selector: 'hello',
    template: `<div [innerHTML]="myHtmlString | escapeHtml"></div>`,
    pipes : [EscapeHtmlPipe]
})
export class Hello {
  constructor() {
    this.myHtmlString = "<b>This is some bold text</b>";
  }
}

Here's a plnkr with a naive html escaping/parsing.

I hope it helps :)

Ashford answered 2/11, 2015 at 11:7 Comment(5)
Looks like the syntax is now [innerHTML]: https://mcmap.net/q/53071/-angular-html-bindingHassan
This isn't sufficient for cases where your innerHTML actually contains angular-usable code. In my case, I want to be able to use [routerLink], but I can't.Gustafsson
@thouliha did you ever figure out a solution to that?Indigestive
@Indigestive nope. Once you use [innerHTML], you lose all templating abilities. Angular seems to really want you to do all your templating directly in static html.Gustafsson
the solution in the plnkr is only for <b> tag, how to handle for any(all) html tag?Novitiate
M
35

Simplest solution:

<div [innerHTML]="some_string"></div>

Where some_string can be html code, e.g: some_string = "<b>test</b>".

No pipes or anything needed. Supported by Angular 2.0

Meteoritics answered 23/9, 2016 at 9:6 Comment(3)
How can I have two space-separated properties inside innerHTML. Like prop1_value prop2_value. Without creating any method to concat them.Reunionist
Try doing something like [innerHTML]="{prop1_value + ' ' + prop2_value}"Meteoritics
It didn't work like this. I added a new span and added that prop value there.Reunionist
A
30

In angular2 there's no ng-include, trustAsHtml, ng-bind-html nor similar, so your best option is to bind to innerHtml. Obviously this let you open to all kind of attacks, so it's up to you to parse/escape the content and for that you can use pipes.

@Pipe({name: 'escapeHtml', pure: false})
class EscapeHtmlPipe implements PipeTransform {
   transform(value: any, args: any[] = []) {
       // Escape 'value' and return it
   }
}

@Component({
    selector: 'hello',
    template: `<div [innerHTML]="myHtmlString | escapeHtml"></div>`,
    pipes : [EscapeHtmlPipe]
})
export class Hello {
  constructor() {
    this.myHtmlString = "<b>This is some bold text</b>";
  }
}

Here's a plnkr with a naive html escaping/parsing.

I hope it helps :)

Ashford answered 2/11, 2015 at 11:7 Comment(5)
Looks like the syntax is now [innerHTML]: https://mcmap.net/q/53071/-angular-html-bindingHassan
This isn't sufficient for cases where your innerHTML actually contains angular-usable code. In my case, I want to be able to use [routerLink], but I can't.Gustafsson
@thouliha did you ever figure out a solution to that?Indigestive
@Indigestive nope. Once you use [innerHTML], you lose all templating abilities. Angular seems to really want you to do all your templating directly in static html.Gustafsson
the solution in the plnkr is only for <b> tag, how to handle for any(all) html tag?Novitiate
A
4

I got Same Problem buy I Request the decode HTML from Backend and them you can inject html to your page

// YOUR TS
@Component({
  selector: 'page',
  templateUrl: 'page.html'
})
export class Page {
  inject:any;
  constructor( ) { }

  ionViewDidLoad() {
    this.inject='your HTML code'

  }

}
// YOU HTML PAGE

<div [innerHTML]="inject"></div>
Astroid answered 29/11, 2016 at 21:5 Comment(0)
P
0

The best solution which can be of your help is as below:

<p [innerHTML]=your_response_which_is_string></p>

Hope it helps!!!

Pancreatotomy answered 13/12, 2019 at 10:36 Comment(0)
I
-1

For property binding use below : <div innerHtml="{{ property }}"></div>

For just a string : <div [innerHtml]="<p>property</p>"></div>

Islas answered 14/6, 2017 at 6:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.