gpt4 book ai didi

angular - 如何将 bootstrap popover 绑定(bind)添加到我的 HTML?

转载 作者:太空狗 更新时间:2023-10-29 17:50:53 25 4
gpt4 key购买 nike

我有一个 SVG,它里面有一个 rect 元素,点击 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

最佳答案

我不确定这是否可以以 Angular 方式完成,即 ng-bootstrap。或者,您可以使用简单的 Bootstrap 并实用地添加弹出窗口。因此,首先在您的 index.html 页面上添加 Bootstrap 所需的文件(我已经跳过添加 css,因为 css 已经包含在 ndb 中)

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

然后在你的组件中

declare var $ ;

$(react).popover({content:'Rectangle was clicked'});

Working Stackblitz

关于angular - 如何将 bootstrap popover 绑定(bind)添加到我的 HTML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57877609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com