1

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: undefined のプロパティ 'hide' を読み取れません

PopoverContent.hide (PopoverContent.js:78) で

「PopoverContent.js」ファイルには、行 this.popover.hide(); があります。しかし、私はそれを初期化する方法がわかりません。私の理解では、@ViewChild は #customPopover へのクラス バインドのみを初期化します。つまり、私の場合は popover-content です。「ポップオーバー」を初期化するための解決策を教えてください。

4

2 に答える 2

2

以下のコードを使用して解決しました。つまり、関数のパラメーターとして「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();
}
于 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 に答える