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