Angular2 ng2-popover hide() 不工作

Angular2 ng2-popover hide() is not working

app.module.ts

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

example.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>

example.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 类型错误:无法读取未定义的 属性 'hide'

在 PopoverContent.hide (PopoverContent.js:78)

在 'PopoverContent.js' 文件中有行 this.popover.hide();但我不知道如何初始化它。据我了解,@ViewChild 仅初始化 class 绑定到 #customPopover,即在我的例子中是 popover-content。有人可以给我一个初始化 'Popover' 的解决方案吗?

我认为在你的情况下,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>

看看这是否有帮助。

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

example.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();
}