Angular2 ng2-popover hide()不起作用

时间:2016-09-25 08:56:48

标签: javascript angular ionic-framework angular2-routing ng2-bootstrap

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

我希望在模态打开时隐藏弹出窗口。当我打电话给&#39; customPopover.hide()&#39;它给我错误的功能:

error_handler.js:51 TypeError:无法读取属性&#39;隐藏&#39;未定义的

在PopoverContent.hide(PopoverContent.js:78)

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

的解决方案

2 个答案:

答案 0 :(得分:2)

我使用下面的代码解决了它,即添加&#39; customPopover&#39;作为函数中的参数并调用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();
}

答案 1 :(得分:0)

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

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

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

看看这是否有帮助。