【发布时间】:2019-09-10 20:11:31
【问题描述】:
我有一个SVG,里面有一个rect 元素,点击矩形时我会看到一个警报。
我想用bootstrap popover 替换它,就像下面的按钮,我该怎么做?它必须是代码而不是 HTML
这是我要解决的问题的Stackblitz
import { Component, Input, AfterViewInit, ViewChild, ElementRef, Renderer2 } from '@angular/core';
@Component({
selector: 'hello',
template: `
<svg #mySvg width="400" height="400">
</svg>
<br> <br><br>
<button type="button" class="btn btn-outline-secondary mr-2" placement="right"
ngbPopover="Vivamus sagittis lacus vel augue laoreet rutrum faucibus." popoverTitle="Popover on top">
Should Mimic this
</button>
`,
styles: [`svg {
border: 1px solid #000000;
}`]
})
export class HelloComponent implements AfterViewInit {
@Input() name: string;
@ViewChild('mySvg', { static: false }) mySvg: ElementRef;
constructor(private renderer: Renderer2) { }
ngAfterViewInit() {
const svg = this.mySvg.nativeElement;
const svgNS = svg.namespaceURI;
const rect = this.renderer.createElement('rect', svgNS);
const clickedOnRect = () => {
alert('Rectangle was clicked');
}
rect.setAttributeNS(null, 'x', '100');
rect.setAttributeNS(null, 'y', '100');
rect.setAttributeNS(null, 'width', '100');
rect.setAttributeNS(null, 'height', '100');
rect.setAttributeNS(null, 'fill', "transparent");
rect.setAttributeNS(null, 'stroke', "red");
rect.setAttributeNS(null, 'stroke-width', '5');
rect.setAttributeNS(null, 'tab-index', '1');
rect.setAttributeNS(null, 'cursor', 'pointer');
rect.addEventListener('click', ($event) => {
clickedOnRect();
});
svg.appendChild(rect);
}
}
注意:我想将弹出框附加到 rect 元素而不是 svg
【问题讨论】:
-
为什么不直接将属性应用到
svg元素?似乎工作正常:angular-irou2w.stackblitz.io -
在我的应用程序中,我在 svg 中绘制了不同的形状,每个形状都必须有一个弹出框,所以在整个 svg 上它不会工作
标签: angular twitter-bootstrap svg ng-bootstrap