简体   繁体   中英

how to display html string(html tags) as content in a textarea?

I want to display the html tags in a div tag so i used like,

<p [innerHTML]="data"> // data is a html tags for ex: h1 tag
</p>

it is working fine but if i want to print in the text area, it is not translating the html code and directly printing the html code like,

<textarea  rows="4" cols="50">
    {{data}}
</textarea>

Here is the working code on stackblitz,

<textarea> is input element, you cannot bind HTML tags like <b> or <i> in it.

To bind the data remove this HTML tags from typescript and bind it in <textarea> using [(ngModel)]

Try like this:

<textarea  rows="4" cols="50" [(ngModel)]="data">
</textarea>

This is not possible, what you are looking for is a content editable div. Look at this fiddle , but basically you need to (just translate this to Angular):

<div contenteditable="true">This is the first line.<br>
    See, how the text fits here, also if<br>there is a <strong>linebreak</strong> at the e nd?
    <br>It works nicely.
    <br>
    <br><span style="color: lightgreen">Great</span>.
</div>

This question already has answer in stackblitz, Rendering HTML inside textarea

What you are doing is, set the div tag editable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM