简体   繁体   中英

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:

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

And I'm adding < b > tag to the string:

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 >.' in UI.

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

I tried to encode the bracket but id didn't work; Are there any way that I can make it partial bold?

I think you probably need to use innerHTML:

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

You can check the documentation for more information

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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