简体   繁体   English

Angular 2 材料工具提示 HTML 内容在 angular

[英]Angular 2 Material tooltip with HTML content in angular

I am trying to bind the HTML strings into angular 2 tool tip.我正在尝试将 HTML 字符串绑定到 angular 2 工具提示中。 But it's rendering as HTML it showing as a plain string.但它呈现为 HTML 它显示为纯字符串。 Is there any way to do render string as an HTML.有什么办法可以将字符串渲染为 HTML。

Here is my code:这是我的代码:

In View:在视图中:

 <span class="icon-status" mdTooltip="{{value.status}}"></span>

Component:成分:

value.status = this.sanitizer.bypassSecurityTrustHtml(this.getStatusTemplate(value.status));


  getStatusTemplate(status: IIconStatus): string{
let HTML =`
  <div>Event Title</div>
  <div class="">
    <table>
      <thead>
        <th>User</th>
        <th>User</th>
        <th>User</th>
        <th>User</th>
      </thead>
    </table>
  </div>
`;
return HTML;}

Thanks in advance.提前致谢。

Bad news for us: HTML is not supported in angular material tooltips and for now, they don't have intentions to support it.对我们来说是个坏消息:角度材料工具提示不支持 HTML,目前,他们无意支持它。 Here more info.这里有更多信息。

It's not well tested, but I did it by defining setter for "message" property on TooltipComponent(app.component.ts)它没有经过很好的测试,但我通过在 TooltipComponent(app.component.ts) 上为“message”属性定义 setter 来实现的

Object.defineProperty(TooltipComponent.prototype, 'message', {
   set(v: any) {
       const el = document.querySelectorAll('.mat-tooltip');

       if (el) {
           el[el.length - 1].innerHTML = v;
       }
   },
});

html html

<div [matTooltipClass]="'right'"
     [matTooltip]="aboveTemplate"
     matTooltipPosition="right">
    Right
</div>

ts ts

aboveTemplate = `<div class="color-red">Top Template<button style="background: white; color: black">Press me</button></div>`;

result结果

在此处输入图片说明

i did this, that works perfctly 我做到了,很完美

html file : html文件:

<span class="icon-status" [mdTooltip]="getStringFromHtml(value.status)"></span>

ts file : ts文件:

   getStringFromHtml(text){
      const html = text;
      const div = document.createElement('div');
      div.innerHTML = html;
      return div.textContent || div.innerText || '';
}

Material, as opposed to bootstrap, does not have anything like a popover (it has tooltip which is just text, no HTML). Material 与 bootstrap 不同,它没有像 popover 这样的东西(它的工具提示只是文本,没有 HTML)。 I think based on the guides for this use-case you are supposed to use a dialog instead.我认为根据此用例的指南,您应该改用对话框。

What you are looking for is a Popover .您正在寻找的是Popover And as said, it doesn't exist now and it's not possible with tooltips.如前所述,它现在不存在,工具提示也不可能。

Answer from @jelbourn , Angular member team: Angular 成员团队@jelbourn 的回答:

When designing the tooltip we deliberately decided not to support this.在设计工具提示时,我们故意决定不支持这一点。 The Material Design spec is rather prescriptive about only text appearing in tooltips. Material Design 规范对仅出现在工具提示中的文本有相当的规定。 Rich content also presents a challenge for a11y.丰富的内容也给 a11y 带来了挑战。

Source: https://github.com/angular/components/issues/5440#issuecomment-313740211来源: https : //github.com/angular/components/issues/5440#issuecomment-313740211

You can find the feature request for popover here .您可以在此处找到 popover 的功能请求。

Until an official release from Material team you can use an alternative.在 Material 团队正式发布之前,您可以使用替代方案。 Here are some examples:以下是一些示例:


Edit : If you need simple customization (changing background-color, color, font-size...) for the whole tooltip you can read this post .编辑:如果您需要对整个工具提示进行简单的自定义(更改背景颜色、颜色、字体大小...),您可以阅读这篇文章

I believe what you want is CDK Overlay:我相信你想要的是 CDK Overlay:

https://material.angular.io/cdk/overlay/overview https://material.angular.io/cdk/overlay/overview

There are some examples there.那里有一些例子。

How to test passed it by jest?如何通过开玩笑测试?

Object.defineProperty(TooltipComponent.prototype, 'message', {
   set(v: any) {
       const el = document.querySelectorAll('.mat-tooltip');

       if (el) {
           el[el.length - 1].innerHTML = v;
       }
   },
});

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

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