1

app.module.tsAngular2 NG2-酥料餅皮()不工作

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類型錯誤:無法讀取的不確定

在PopoverContent.hide(PopoverContent.js屬性 '隱藏':78 )

在'PopoverContent.js'文件中有this this.popover.hide();但我不知道如何初始化它。正如我的理解是@ViewChild只初始化類綁定到#customPopover,即在我的情況彈出內容。有人可以給我一個解決方案來初始化'Popover'嗎?

回答

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> 

看看這會有所幫助。

+0

感謝@Sanket給了一個線索。是'this.customPopover'未定義。我使用下面的代碼解決了它。 –

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