【问题标题】:How to make text bold in string literal in angular?如何在角字符串中制作文本粗体?
【发布时间】:2021-05-15 00:42:02
【问题描述】:

您好,我正在开发 Angular 应用程序。我想将文本的某些部分加粗,那么如何在字符串文字中做到这一点。

这是 .ts 类中的代码:

   if (res.status== "200") {
    this.colortext = `${res.colorofthebus} color is the best of ${res.othercolor}`;
  }

这是.html:

{{colortext}}

如何使res.colorofthebus 变为粗体,而其他字体变为普通字体。

TIA。

【问题讨论】:

标签: javascript angular angular6 angular8


【解决方案1】:

使用innerHTML。参考Documentation

if (res.status== "200") {
    this.colortext = `<b>${res.colorofthebus}</b> color is the best of ${res.othercolor}`;
  }

...

<div [innerHTML]="colortext"></div>

【讨论】:

    【解决方案2】:

    一个可能的解决方案是使用angular的innerHtml,像这样:

    .ts

    this.colortext = `<strong>${res.colorofthebus}</strong> color is the best of ${res.othercolor}`;
    

    .html

    <span [innerHTML]="colortext"></span>
    

    【讨论】:

      【解决方案3】:

      html

      <b>{{colortext?.colorofthebus}}</b> color is the best of {{colortext?.othercolor}}
      

      .ts

      this.colortext = res

      【讨论】:

        猜你喜欢
        • 2018-04-15
        • 2022-11-09
        • 1970-01-01
        • 1970-01-01
        • 2020-08-08
        • 2018-05-22
        • 1970-01-01
        • 2018-03-02
        • 1970-01-01
        相关资源
        最近更新 更多