2

I'm trying to render an html string so that it displays links, etc using Ionic. This is my very first Ionic app.

<ion-content class="home">
<ion-list>
  <ion-item *ngFor="let f of item">
    <h2 class="question" style="white-space:initial;" ng-bind-html="{{f.question}}">{{f.question}}</h2>
    <p class="answer" style="white-space:initial;" ng-bind-html="{{f.answer}}">{{f.answer}}</p>
  </ion-item>
</ion-list>
</ion-content>

I've tried ng-bind-html as above and it's saying...

Unhandled Promise rejection: Template parse errors: Can't bind to 'ng-bind-html' since it isn't a known property of 'h2'. ("

I've also tried ng-bind-html-safe but that produces the same error. Without the ng-bind-html code, the string is displayed but with the html characters not being interpreted.

Also, here is the code coming from the .ts file

  this.faqService.load()
  .then(data1 => { 
    this.item = data1;
  });
7
  • stackoverflow.com/questions/31548311/angular-2-html-binding Commented Mar 7, 2017 at 5:40
  • 1
    ng-bind-html is in angular 1. not sure why you need to bind in your example though. Commented Mar 7, 2017 at 5:41
  • So what is the replacement in 2? Commented Mar 7, 2017 at 5:42
  • check the link you just do <p [innerHTML]="{{f.answer}}"> </p> Commented Mar 7, 2017 at 5:43
  • 3
    ohk try <p [innerHTML]="f.answer"> </p> Commented Mar 7, 2017 at 5:48

1 Answer 1

4

The answer to my question is

<p [innerHTML]="f.answer"></p> 

Hope this helps other newbies to Angular 2 / Ionic.

Sign up to request clarification or add additional context in comments.

1 Comment

This allows for cross site scripting You should use it very sparingly.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.