简体   繁体   English

Angular matTooltip呈现为纯文本

[英]Angular matTooltip rendering as plain text

I am iterating through a JSON document in angular 7, where each iteration object has a sentence and a list of words. 我正在迭代角度7中的JSON文档,其中每个迭代对象都有一个句子和一个单词列表。 Then, I'm trying to add a tooltip for each of the matched words in the sentence using the following method, 然后,我正在尝试使用以下方法为句子中的每个匹配单词添加工具提示,

let sentence = ''';
addTooltip(word: string){
    let updatedWord = '<span matTooltip="word decription">'+word+'</span>';
    return this.sentence.replace(new RegExp(word, 'gi'), updatedWord);
}

In HTML, instead of rendering the updated words as tooltip, its just showing the full span tag in plain text, like this, 在HTML中,不是将更新后的单词呈现为工具提示,而是以纯文本形式显示完整的span标记,如下所示,

<span matTooltip="word decription">word</span>

But When I'm placing the same line directly in HTML, its perfectly showing the tooltip. 但是当我将相同的行直接放在HTML中时,它完美地显示了工具提示。

Do I need to add any additional code in the typescript or css in order to make it work? 我是否需要在打字稿或css中添加任何其他代码才能使其正常工作?

As I can understand you want to add HTML markup from component to template. 据我所知,您希望从组件到模板添加HTML标记。

For that you can bind to it and apply the markup with something like this in your template using innerHTML property 为此,您可以绑定它并使用innerHTML属性在模板中应用类似的标记

<div [innerHTML]="sentence"></div>

Hope this helps you :) 希望这可以帮助你:)

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

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