簡體   English   中英

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 TypeError:無法讀取未定義的屬性“ hide”

在PopoverContent.hide(PopoverContent.js:78)

在“ PopoverContent.js”文件中,有一行this.popover.hide();。 但我不知道如何初始化它。 據我了解,@ViewChild僅將類綁定初始化為#customPopover,即在我的情況下為popover-content。 有人可以給我解決方案初始化“ Popover”嗎?

我使用以下代碼解決了該問題,即在函數中添加“ 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();
}

我認為在您的情況下, 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>

看看是否有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM