gpt4 book ai didi

javascript - 在字符串 Angular 的一部分上应用粗体文本

转载 作者:行者123 更新时间:2023-12-01 16:04:43 25 4
gpt4 key购买 nike

我想让我的部分文字加粗。

我从特定文件中获取文本。
"INFORMATION": "Here's an example of text",

我想要 Here's an要大胆。
"INFORMATION": "<b>Here's an</b> example of text","INFORMATION": "<strong>Here's an</strong> example of text"
然后我打印出来

<span translate>INFORMATION</span>

而不是得到

这是 文本示例

我明白了
<b>Here's an</b> example of text

或者
<strong>Here's an</strong> example of text

更新

我正在尝试innerHTML
<span [innerHTML]="information | translate"></span>

信息是包含文本的变量

但它忽略了我的 html 标签,它只打印文本

最佳答案

我要做的是一个管道来清理你给它的字符串,并使用正则表达式使其更通用。像这样的堆栈 Blitz :

https://stackblitz.com/edit/angular-tyz8b1?file=src%2Fapp%2Fapp.component.html

import { Pipe, PipeTransform, Sanitizer, SecurityContext } from '@angular/core';

@Pipe({
name: 'boldSpan'
})
export class BoldSpanPipe implements PipeTransform {

constructor(
private sanitizer: Sanitizer
) {}

transform(value: string, regex): any {
return this.sanitize(this.replace(value, regex));
}

replace(str, regex) {
return str.replace(new RegExp(`(${regex})`, 'gi'), '<b>$1</b>');
}

sanitize(str) {
return this.sanitizer.sanitize(SecurityContext.HTML, str);
}
}

这样,变量内容实际上并没有改变,这意味着您的数据保持不变。

关于javascript - 在字符串 Angular 的一部分上应用粗体文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55241693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com