简体   繁体   English

带角度2应用程序的自举弹出窗口

[英]Bootstrap popover with angular 2 App

In the angular 2 app, I'm displaying a popover when a button clicked using jQuery. 在angular 2应用程序中,当使用jQuery单击按钮时,我正在显示一个弹出窗口。 But typescript throwing an error 但是打字稿抛出错误

 Object doesn't support property or method 'popover'

My component 我的组件

/// <reference path="../shared/bootstrap.ts" />


 import { Component, OnInit } from '@angular/core';
 import * as $ from 'jquery';


  @Component({
    selector: 'my-calender',
    templateUrl: `
       <a class="btn btn-success btn-sm" (click)="openPopover()">Disply Popup</a>
     `
  })

export class CalenderComponent implements OnInit {  

    openPopover(): void {

       var that: any = this;

        $(that).popover({
        title: 'My Popover'

       });
     }  

Typescript compilation is fine. 打字稿编译很好。 But when I run the app and click that button to shows the popup, it throwing the above error. 但是,当我运行该应用程序并单击该按钮以显示弹出窗口时,它将引发上述错误。

But Other boostrap default functionalities are working as expected (nav bar, dropdown). 但是其他boostrap默认功能正在按预期运行(导航栏,下拉菜单)。

this is not that.. this是不是说..

this refers to the CalendarComponent class. this是指CalendarComponent类。 I suggest looking a little more at the tutorials. 我建议多看一些教程。 TypeScript/ES6 has the neat feature that you don't really have to worry about the this context any more. TypeScript / ES6具有简洁的功能,您实际上不必再担心this上下文。 As long as you use () => {} arrow function notations. 只要使用() => {}箭头功能符号即可。

In the meantime you can try something like this: 在此期间,您可以尝试执行以下操作:

@Component({
    selector: 'my-calender',
    templateUrl: `
       <a class="btn btn-success btn-sm" (click)="openPopover($event.currentTarget)">Disply Popup</a>
     `
})    
export class CalenderComponent implements OnInit {  

    openPopover(target: HTMLElement): void {
       $(target).popover({
        title: 'My Popover'
       });
    }
} 

You can access the event property within a template using the $event variable. 您可以使用$event变量访问模板内的event属性。 This will contain a MouseEvent on click. 单击将包含一个MouseEvent MouseEvent has a currentTarget property which contains the HtmlElement on which the event was placed. MouseEvent具有currentTarget属性,该属性包含放置事件的HtmlElement。 From there you can intialise your jQuery function. 从那里您可以初始化您的jQuery函数。

Another idea could be to use @ViewChild , but you will find plenty of examples if you check out the guides and cookbooks at angular.io . 另一个想法可能是使用@ViewChild ,但是如果您在angular.io上查看指南和菜谱,将会发现很多示例。

edit 编辑

If after all this it still doesn't work, you should make sure you added the tooltip plugin ( tooltip.js ) to your project. 如果毕竟这仍然不起作用,则应确保已将tooltip插件( tooltip.js )添加到项目中。 Bootstrap needs this to enable popover 引导程序需要此来启用popover

In Angular context it is better to use a dedicated, native libraries that provide much better API for the Angular users. 在Angular上下文中,最好使用专用的本机库,这些库为Angular用户提供更好的API。 There is an excellent implementation for Angular and Bootstrap 4: https://ng-bootstrap.github.io . Angular和Bootstrap 4有一个出色的实现: https : //ng-bootstrap.github.io By using it you could skip the whole jQuery / Bootstrap JS story. 通过使用它,您可以跳过整个jQuery / Bootstrap JS故事。

Using Bootstrap's JS means that you would have to load jQuery, Bootstrap's JS and potentially create Angular wrappers around Bootstrap's JS code. 使用Bootstrap的JS意味着您将不得不加载jQuery,Bootstrap的JS并可能围绕Bootstrap的JS代码创建Angular包装器。 But even doing so would result in rather poor integration since both jQuery and Angular would "fight" over DOM updates - the philosophy of those 2 libraries is very different. 但是即使这样做,也会导致集成效果很差,因为jQuery和Angular都将“争夺” DOM更新-这两个库的理念非常不同。

On the other hand using popovers from ng-bootstrap is very easy: 另一方面,使用ng-bootstrap中的弹出窗口非常容易:

<button type="button" class="btn btn-secondary" placement="top"
        ngbPopover="Vivamus sagittis lacus vel augue laoreet rutrum faucibus." popoverTitle="Popover on top">
  Popover on top
</button>

In the above example ngbPopover directive is all that is needed! 在上面的示例中,仅需要ngbPopover指令! You can see in action in a working plunk http://plnkr.co/edit/MiUo2BOPJCVkiVxRT6or?p=preview and check more examples / documentation on https://ng-bootstrap.github.io/#/components/popover 您可以在工作正常的http://plnkr.co/edit/MiUo2BOPJCVkiVxRT6or?p=preview中查看操作,并在https://ng-bootstrap.github.io/#/components/popover上查看更多示例/文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM