Àle*_*x P 3 ionic-framework ionic-popover ionic2 viewchild
我有一个页面组件,其中包含一个打开PopoverController的按钮.根据Ionic Docs,弹出窗口需要另一个特定的内容组件.
在主页面中,我有一个需要从弹出窗口组件调用的函数,但我还没有找到如何访问"父级"方法.我试过@ViewChild但孩子却是undefined.
import { Component, ViewChild } from '@angular/core';
import { Content, NavController, NavParams, Platform, PopoverController, ViewController } from 'ionic-angular';
// Parent page:
@Component({
selector: 'page-favorites',
templateUrl: 'favorites.html'
})
export class FavoritesPage {
constructor(public popoverCtrl: PopoverController) {
}
openOptions(ev?: Event) {
let popover = this.popoverCtrl.create(FavoritesPopover);
popover.present({ ev: ev });
}
showConfirm() {
// This is the function I need to call from FavoritesPopover
}
}
// Popover content:
@Component({
template: `
<ion-list>
<button ion-item (click)="showConfirm()">Show confirm</button>
</ion-list>`
})
export class FavoritesPopover {
@ViewChild(FavoritesPage) favoritesPage: FavoritesPage;
showConfirm() {
this.favoritesPage.showConfirm(); // It doesn't work, favoritesPage is undefined
}
}
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,我刚刚开始使用Ionic 2和Angular,所以任何帮助都会非常感激!
Sch*_*aus 11
您可以通过将对象作为第二个参数传递给create方法,将数据(和函数)传递给popover :
openOptions(ev?: Event) {
let popover = this.popoverCtrl.create(FavoritesPopover, {
showConfirm: function() {
alert('yay');
}
});
popover.present({ ev: ev });
}
Run Code Online (Sandbox Code Playgroud)
然后,您应该注入NavParams弹出视图和get您传递的函数:
import {NavParams} from 'ionic-angular';
export class FavoritesPopover {
constructor(public params: NavParams) {}
showConfirm() {
this.params.get('showConfirm')();
}
}
Run Code Online (Sandbox Code Playgroud)