简体   繁体   English

如何将 html 字符串(html 标签)显示为文本区域中的内容?

[英]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,我想在 div 标签中显示 html 标签,所以我用过,

<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,它工作正常,但如果我想在文本区域打印,它不会翻译 html 代码并直接打印 html 代码,例如,

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

Here is the working code on stackblitz,这是stackblitz上的工作代码

<textarea> is input element, you cannot bind HTML tags like <b> or <i> in it. <textarea>是输入元素,您不能在其中绑定<b><i>类的 HTML 标签。

To bind the data remove this HTML tags from typescript and bind it in <textarea> using [(ngModel)]要绑定数据,请从打字稿中删除此 HTML 标记并使用[(ngModel)]将其绑定在<textarea>

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.这是不可能的,您正在寻找的是内容可编辑的 div。 Look at this fiddle , but basically you need to (just translate this to Angular):看看这个fiddle ,但基本上你需要(只需将其转换为 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这个问题在 stackblitz, Rendering HTML inside textarea 中已经有了答案

What you are doing is, set the div tag editable您正在做的是,将 div 标签设置为可编辑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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