如何将 bootstrap popover 绑定添加到我的 HTML 中?

dot*_*pro 5 svg twitter-bootstrap ng-bootstrap angular

我有一个里面SVG有一个rect元素,点击矩形我会看到一个警报。

我想用引导弹出窗口替换它,如下面的按钮,我该怎么做?它必须是代码而不是 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);
  }
}
Run Code Online (Sandbox Code Playgroud)

注意:我想将弹出框附加到rect元素上,而不是svg

jit*_*der 4

我不确定这是否可以以角度方式完成,即 ng-bootstrap。或者,您可以使用简单的引导程序并务实地添加弹出窗口。因此,首先在您的index.html页面上添加引导程序所需的文件(我已经添加了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>
Run Code Online (Sandbox Code Playgroud)

然后在你的组件中

declare var $ ;

$(react).popover({content:'Rectangle was clicked'});
Run Code Online (Sandbox Code Playgroud)

工作堆栈闪电战