【发布时间】:2022-01-14 17:50:27
【问题描述】:
我的 Angular 项目有问题。我有一个带有标签的原始文本,例如:Listening to the rain, falling on the streets #Rain #Chill
现在我希望这 2 个标签是按钮,所以我在我的打字稿中有这个:
ngOnInit(): void {
this.description = this.description.replace(/#(\w+)/g, '<button (click)="onHashtagClick($1)" class="hashtag">#$1</button>');
}
onHashtagClick(hashtag:string) {
console.log(hashtag);
}
这导致以下描述:
Listening to the rain, falling on the streets <button (click)="onHashtagClick(Rain)" class="hashtag">#Rain</button> <button (click)="onHashtagClick(Chill)" class="hashtag">#Chill</button>
按钮的 HTML 标记按字面意思打印在屏幕上。我发现在 Angular 中,我需要为此使用 [innerHTML] 标签,所以我的 HTML 变成了:
<label class="description" [innerHTML]="description"></label>
如何制作主题标签的按钮(无超链接)。请让男人知道我做错了什么?!
【问题讨论】:
标签: javascript html angular typescript