1

app.module.ts

import { PopoverModule } from 'ng2-popover';
@NgModule({
  declarations: [ ...],
  imports: [PopoverModule],
  providers: []
})

例子.html

<a [popover]="customPopover" [popoverOnHover]="true" [popoverCloseOnMouseOutside]="true" href="www.google.com" (click)="$event.stopPropagation()" target="_blank">{{name}}</a>
    <!--Popover content -->
    <popover-content #customPopover title="{{name}}" placement="right" 
      [closeOnClickOutside]="true" [closeOnMouseOutside]="true">
      <span class="popoverDesc">{{description}}</span><br /><br />
      <a href="{{websiteLink | formatUrl:'url'}}" (click)="$event.stopPropagation()" target="_blank">{{websiteLink | formatUrl:'text'}}</a><br /><br />
      <button class="btn btn-secondary popoverBtn" (click)="toggleAddToListModalPopover($event)">Add to list</button>
     </popover-content>

例子.component.ts

import { PopoverContent } from 'ng2-popover';

@ViewChild('customPopover') customPopover: PopoverContent;

protected toggleAddToListModalPopover(e):void {
  this.customPopover.hide();
  this.showAddToListModal = !this.showAddToListModal;
  e.stopPropagation();
}

我想在模式打开时隐藏弹出框。当我调用“customPopover.hide()”函数时,它给了我错误:

error_handler.js:51 TypeError:无法读取未定义的属性“隐藏”

在 PopoverContent.hide (PopoverContent.js:78)

在“PopoverContent.js”文件中有一行 this.popover.hide(); 但我不知道如何初始化它。正如我的理解是@ViewChild 只初始化类绑定到#customPopover 即在我的情况下popover-content。有人可以给我一个初始化“Popover”的解决方案吗?

4

2 回答 2

2

我使用下面的代码解决了它,即在函数中添加“customPopover”作为参数并调用 hide() 方法。我不知道这是否是解决这个问题的好方法?

例子.html

<button class="btn btn-secondary popoverBtn" (click)="toggleAddToListModalPopover(customPopover, $event)">Add to list</button>

example.component.ts:

protected toggleAddToListModalPopover(customPopover, e):void {
    customPopover.hide();
    this.showAddToListModal = !this.showAddToListModal;
    e.stopPropagation();
}
于 2016-09-25T09:58:34.033 回答
0

我认为在你的情况下,this.customPopover是未定义的。

其他方式你可以像这样隐藏你的弹出内容 -

  <div>
    <popover-content #myPopover title="this header can be omitted" placement="right" [closeOnClickOutside]="true">
      <b>Very</b> <span style="color: #C21F39">Dynamic</span> <span style="color: #00b3ee">Reusable</span>
      <b><i><span style="color: #ffc520">Popover With</span></i></b> <small>Html support</small>. Click outside of this popover and it will be dismissed automatically.

      <u (click)="myPopover.hide()">Or click here to close it</u>.
    </popover-content>

    <button [popover]="myPopover">click this button to see a popover</button>
  </div>

看看这是否有帮助。

于 2016-09-25T09:24:09.140 回答