[英]Angular 2 Material tooltip with HTML content in angular
我正在嘗試將 HTML 字符串綁定到 angular 2 工具提示中。 但它呈現為 HTML 它顯示為純字符串。 有什么辦法可以將字符串渲染為 HTML。
這是我的代碼:
在視圖中:
<span class="icon-status" mdTooltip="{{value.status}}"></span>
成分:
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;}
提前致謝。
對我們來說是個壞消息:角度材料工具提示不支持 HTML,目前,他們無意支持它。 這里有更多信息。
它沒有經過很好的測試,但我通過在 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
<div [matTooltipClass]="'right'"
[matTooltip]="aboveTemplate"
matTooltipPosition="right">
Right
</div>
ts
aboveTemplate = `<div class="color-red">Top Template<button style="background: white; color: black">Press me</button></div>`;
結果
我做到了,很完美
html文件:
<span class="icon-status" [mdTooltip]="getStringFromHtml(value.status)"></span>
ts文件:
getStringFromHtml(text){
const html = text;
const div = document.createElement('div');
div.innerHTML = html;
return div.textContent || div.innerText || '';
}
Material 與 bootstrap 不同,它沒有像 popover 這樣的東西(它的工具提示只是文本,沒有 HTML)。 我認為根據此用例的指南,您應該改用對話框。
您正在尋找的是Popover 。 如前所述,它現在不存在,工具提示也不可能。
Angular 成員團隊@jelbourn 的回答:
在設計工具提示時,我們故意決定不支持這一點。 Material Design 規范對僅出現在工具提示中的文本有相當的規定。 豐富的內容也給 a11y 帶來了挑戰。
來源: https : //github.com/angular/components/issues/5440#issuecomment-313740211
您可以在此處找到 popover 的功能請求。
在 Material 團隊正式發布之前,您可以使用替代方案。 以下是一些示例:
編輯:如果您需要對整個工具提示進行簡單的自定義(更改背景顏色、顏色、字體大小...),您可以閱讀這篇文章。
如何通過開玩笑測試?
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.