【问题标题】:How to add a html element to Value in Array JSON, Typescript, Angular如何将 html 元素添加到数组 JSON、Typescript、Angular 中的值
【发布时间】:2021-05-10 15:37:15
【问题描述】:

我想将一些动态数据添加到我的 Angular 项目中,我有一个包含数据的 JSON 文件,并且我从打字稿中获取它并在我的 html 中使用它。我想将一个 HTML 元素存储在一个值中,然后在我的 html 中使用它,所以我做到了,它只是向我显示了结果中的 HTML 元素,例如:
如果我写

[
 {
  "element": "<h1>Element<h1>"
 }
]

那么浏览器中的结果是:

<h1>Element<h1>

而不是显示h1 元素

将不胜感激!

【问题讨论】:

  • 例如: 应该可以解决问题..
  • 没有工作给了我一个错误,我确实喜欢这个&lt;div [innerHtml]="{{ data.content }}"&gt;&lt;/div&gt;

标签: html arrays json angular typescript


【解决方案1】:

试试innerHtml

<div *ngFor="let li of list">
  <div [innerHTML]="li.elem"></div>
</div>

ts

  list = [{
    elem: "<h3>Hello</h3>"
  }, {
    elem: "<h3>World</h3>"
  }];

示例:https://stackblitz.com/edit/angular-ivy-qbg51b?file=src%2Fapp%2Fapp.component.ts

【讨论】:

  • 不,它不起作用,它只是以普通文本格式显示标签内的文本,就像我在其中放置 &lt;button&gt; 时一样,它显示的文本不是按钮,而是像普通文本一样没有任何样式
  • 它也不适用于您提供的代码:photos.google.com/share/… 请帮助...但无论如何感谢您花时间回答我的问题
  • 请告诉我其他方式
猜你喜欢
  • 2019-03-28
  • 1970-01-01
  • 2017-02-11
  • 2016-12-31
  • 2022-11-02
  • 2022-01-02
  • 1970-01-01
  • 2021-08-14
  • 1970-01-01
相关资源
最近更新 更多