为什么 Angular matTooltip 在动态添加时不起作用?
Why doesn't Angular matTooltip work when you add it dynamically?
我有一个 SVG 元素,我想在 Angular 的某个位置添加一个 matTooltip。我观察到如果我尝试像这样添加 matTooltip:
generate() {
var svgEle = document.getElementById("testSVG");
var rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
rect.setAttribute('id', "rect");
rect.setAttribute('x', "73");
rect.setAttribute('y', "95");
rect.setAttribute('class', "st01");
rect.setAttribute('width', "407");
rect.setAttribute('height', "328");
rect.setAttribute('matTooltip', "Info about the action");
svgEle.append(rect)
}
使用html测试代码:
<div style="width:400px">
<svg version="1.1" id="testSVG" xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 1000 1000" style="enable-background:new 0 0 1000 1000;"
xml:space="preserve">
<style type="text/css">
.st01{fill:#F99191;}
.st11{fill:#92B1F7;}
</style>
<rect x="638.5" y="146" class="st11" width="236" height="219"
matTooltip="Info about the action"/>
</svg>
</div>
<button mat-stroked-button (click)="generate()">Generate</button>
没用。
这种情况到底是什么问题?
不行,因为TS中的Angular是编译型语言。
这意味着 [matTooltip]
可能对 TS 编译器有意义,但对 JS 来说是值得的。
Angular 不依赖于元素属性来显示工具提示。相反(如果我没记错的话),它使用动态组件渲染来提供一个绝对定位的丰富组件。
如果你像这样附加工具提示,就等于什么都不做。
您的情况可能有所不同,但这就是我为克服这个问题所做的。为页面创建一个工具提示,并在您滑过某个对象时重新定位它。
在您的 html 文件中
<div #tooltip="matTooltip" matTooltip={{tooltipMessage}}></div>
在您的 .ts 文件中
import {MatTooltip} from '@angular/material';
...
@ViewChild('tooltip') tooltip: MatTooltip;
tooltipMessage: string = '';
.on('mouseover', function(d: Location, event: MouseEvent){
me.tooltipMessage = "My tooltip message."
me.tooltip.disabled = false;
me.tooltip.show();
let tip = me.tooltip._overlayRef.overlayElement;
if(tip){
setTimeout(()=>{
tip.style.left = d.x+"px";
tip.style.top = y+"px";
})
}
})
.on('mouseout', function(d: Location, event: MouseEvent){
me.tooltip.hide();
})
我有一个 SVG 元素,我想在 Angular 的某个位置添加一个 matTooltip。我观察到如果我尝试像这样添加 matTooltip:
generate() {
var svgEle = document.getElementById("testSVG");
var rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
rect.setAttribute('id', "rect");
rect.setAttribute('x', "73");
rect.setAttribute('y', "95");
rect.setAttribute('class', "st01");
rect.setAttribute('width', "407");
rect.setAttribute('height', "328");
rect.setAttribute('matTooltip', "Info about the action");
svgEle.append(rect)
}
使用html测试代码:
<div style="width:400px">
<svg version="1.1" id="testSVG" xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 1000 1000" style="enable-background:new 0 0 1000 1000;"
xml:space="preserve">
<style type="text/css">
.st01{fill:#F99191;}
.st11{fill:#92B1F7;}
</style>
<rect x="638.5" y="146" class="st11" width="236" height="219"
matTooltip="Info about the action"/>
</svg>
</div>
<button mat-stroked-button (click)="generate()">Generate</button>
没用。
这种情况到底是什么问题?
不行,因为TS中的Angular是编译型语言。
这意味着 [matTooltip]
可能对 TS 编译器有意义,但对 JS 来说是值得的。
Angular 不依赖于元素属性来显示工具提示。相反(如果我没记错的话),它使用动态组件渲染来提供一个绝对定位的丰富组件。
如果你像这样附加工具提示,就等于什么都不做。
您的情况可能有所不同,但这就是我为克服这个问题所做的。为页面创建一个工具提示,并在您滑过某个对象时重新定位它。
在您的 html 文件中
<div #tooltip="matTooltip" matTooltip={{tooltipMessage}}></div>
在您的 .ts 文件中
import {MatTooltip} from '@angular/material';
...
@ViewChild('tooltip') tooltip: MatTooltip;
tooltipMessage: string = '';
.on('mouseover', function(d: Location, event: MouseEvent){
me.tooltipMessage = "My tooltip message."
me.tooltip.disabled = false;
me.tooltip.show();
let tip = me.tooltip._overlayRef.overlayElement;
if(tip){
setTimeout(()=>{
tip.style.left = d.x+"px";
tip.style.top = y+"px";
})
}
})
.on('mouseout', function(d: Location, event: MouseEvent){
me.tooltip.hide();
})