简体   繁体   English

如何在JavaScript字符串中加粗部分文本

[英]How to bold partial text in a JavaScript string

I am trying to bold some words in a string, for example, I want the name variable to be bold. 我试图将字符串中的某些单词加粗,例如,我希望名称变量加粗。 In my Angular component, my code is like this: 在我的Angular组件中,我的代码是这样的:

const name = 'Mike';
const message = `My name is ${name}.`

And I'm adding < b > tag to the string: 然后在字符串中添加<b>标签:

const message = `My name is <b>${name}</b>.`

But in my angular template, as I'm passing the message along, it did not bold the string. 但是在我的角度模板中,当我传递消息时,它没有加粗字符串。 Instead, it shows 'My name is < b >Mike< /b >.' 而是显示“我的名字是<b>迈克</ b>”。 in UI. 在用户界面中。

<div>{{message}}</div>

I tried to encode the bracket but id didn't work; 我尝试对括号进行编码,但id无效; Are there any way that I can make it partial bold? 有什么办法可以使它部分粗体?

I think you probably need to use innerHTML: 我认为您可能需要使用innerHTML:

<div [innerHtml]="message"></div>

You can check the documentation for more information 您可以查看文档以获取更多信息

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

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