3

I have been trying to embed html element from JSON object, and It is currently displaying the html as raw code instead.

Can someone please help me ?

detail.ts

 this.questionsData[i].question_type.title = "<ion-input type=\"text\" placeholder=\"Short answer\"></ion-input>";

detail.html

<ion-item  *ngFor="let question of questionsData">
      {{question.title}}
      <br>
      Text : {{question.question_type.title}}
      <br>
     <div   [innerHTML]="question.question_type.title">
    </div>
</ion-item>

example

Isn't it should be input box under the text : , anyone know why it's not rendered ?

Thanks

3 Answers 3

1

InnerHtml will only help to render pure html code on run time, you can't use ionic components to render dynamically using innerhtml. I think you need to use dynamic component loader feature in angular here. You can refer this link on use cases of this feature https://medium.com/@DenysVuika/dynamic-content-in-angular-2-3c85023d9c36

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

Comments

0

Your approach needs to change, do not include html tags in component block, instead add parameters as json data and set it at component. Then you can interpolate the tags in the template view. Ex:

at the component:

this.inputDataArray = [{"title":"What cartoon do you like to watch?", "name":"inp1","placeholder":"short answer"},{"title":"What movie do you like to watch?", "name":"inp2","placeholder":"short answer"}];

at the view:

       <ion-item  *ngFor="let question of inputDataArray">
          {{question.title}}
          <br>
          Text : <input name={{question.name}} placeholder={{question.placeholder}} />
          <br>

    </ion-item>

Its just a reference, Hope it helps

2 Comments

Thank you so much, this approach much cleaner for me, I also use *ngIf now, so the templating is done on html file, not on ts file
Glad to hear that :)
0

This worked for me.

<div [innerHTML]="data.text"></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.