【问题标题】: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