【问题标题】:Ionic2 trigger function of a page in a PopoverPopover中页面的Ionic2触发功能
【发布时间】:2017-09-07 03:55:02
【问题描述】:

我目前正在学习使用 Angular2 和 Ionic2 进行开发,但遇到了一些问题。

我有一个显示地图的主页。在此页面上,我有一个显示弹出框的按钮,其中包含项目列表。 当用户点击一个项目时,它应该触发页面组件中的一个函数以在地图上显示该项目,但我不知道该怎么做。

我尝试在 Popover 中使用事件发射器,但我一定是在某个地方犯了错误,导致主页永远不会收到事件。

以下是我的代码的相关部分:

import { Component, Output, EventEmitter  } from '@angular/core';
import { OnInit } from '@angular/core';

import { NavParams, ViewController } from 'ionic-angular';

import { ShareService } from '../../../app/service/share.service';
import { FavoriService } from '../../../app/service/favori.service';
import { Favori } from '../../../app/classes/favori';
import { Utilisateur } from '../../../app/classes/utilisateur';



@Component({
  templateUrl: 'popover-favori.html',
  providers: [FavoriService]
})

export class PopoverFavori implements OnInit{

  @Output() selectFav: EventEmitter<string> = new EventEmitter<string>();

  constructor(public navParams: NavParams, 
          public viewCtrl: ViewController,
          private favoriService: FavoriService,
          private shareService: ShareService) {}


  listeFavoris: Favori[] = this.shareService.userFavs;
  user: Utilisateur = this.shareService.currentUser;
  selectedFavori: Favori;

  ngOnInit() {
      if (this.navParams.data) {
        this.listeFavoris = this.navParams.data.listeFavori;

      }
  }

selectFavori(favori) {
  this.selectedFavori = favori;
  this.selectFav.emit(favori.nom_point);
;}

我的弹出框模板:

<h2>Favoris</h2>
<ion-list>
  <ion-item-sliding *ngFor="let favori of listeFavoris" >
    <ion-item>
      <h3><ion-icon name="arrow-back"></ion-icon> {{ favori.nom_point }}</h3>
    </ion-item>
    <ion-item-options side="right">
      <button ion-button color="primary" (click)="selectFavori(favori)">
        <ion-icon name="eye"></ion-icon>
      </button>
    </ion-item-options>
  </ion-item-sliding>
</ion-list>

我的页面组件

import { Component, Input, AfterViewInit, ViewChild, OnInit  } from '@angular/core';

import { NavController, NavParams, Platform, ViewController, PopoverController } from 'ionic-angular';
import { FavoriService } from '../../app/service/favori.service';
import { AlertsFavoriService } from '../../app/service/alertsFavori.service';
import { ShareService } from '../../app/service/share.service';
import { UtilisateurService } from '../../app/service/utilisateur.service';
import { Utilisateur } from '../../app/classes/utilisateur';
import { Favori } from '../../app/classes/favori';
import { PopoverFavori } from './popover/popover-favori';


@Component({
  selector: 'page-carte',
  templateUrl: 'carte.html',
  providers: [
    FavoriService
    ]
})
export class CartePage implements OnInit {


  constructor(
    private navCtrl: NavController,
    private popoverCtrl: PopoverController, 
    pprivate navParams: NavParams, 
    private utilisateurService: UtilisateurService,
    private favoriService: FavoriService,
    private shareService: ShareService) { 

  }

   reponseFavoris: Favori[];
   reponseAlertsFavori: AlertsFavori[];
   user = this.shareService.currentUser;

  presentPopoverFavori(ev) {

    let popoverFavori = this.popoverCtrl.create(PopoverFavori, {
      listeFavori : this.reponseFavoris,

        });

    popoverFavori.present({
      ev: ev
    });
  }

 selectedFavoriName: string;

    onFavChange(event) {
      this.selectedFavoriName = event;
      this.getItineraireFavori(this.selectedFavoriName, this.shareService.currentUser.utilisateurId);
    }

页面模板:

<sebm-google-map> //map stuff
</sebm-google-map>
<button ion-fab (click)="presentPopoverFavori($event)" (selectFav)="onFavChange($event)"><ion-icon name="star"></ion-icon></button>

如果有人能提供帮助,将不胜感激。

【问题讨论】:

    标签: angular ionic2 popover


    【解决方案1】:

    我没有发现问题或问题太微妙,所以让我们作弊并使用 ionic2 的事件提供程序而不是 EventEmitter。因为你可以。

    调整popover组件:

      constructor(public navParams: NavParams, 
              public viewCtrl: ViewController,
              private favoriService: FavoriService,
              private shareService: ShareService,
              private events:Events) {}
    
      selectFavori(favori) {
        this.selectedFavori = favori;
        this.events.publish('favori:selected',favori.nom_point);
      }
    

    然后在页面的构造函数中

    constructor(
        private navCtrl: NavController,
        private popoverCtrl: PopoverController, 
        pprivate navParams: NavParams, 
        private utilisateurService: UtilisateurService,
        private favoriService: FavoriService,
        private shareService: ShareService,
        private events:Events) { 
         this.events.subscribe('favori:selected', onFavChange);
      }
    

    【讨论】:

    • 感谢您的建议。它还没有工作,但我知道问题是单击按钮时没有发出事件还是页面没有收到它。你知道我在哪里可以找到 Ionic Events 提供者的工作示例吗?
    【解决方案2】:

    我已经从 ionic 2 实现了事件提供程序。这工作正常。

     import { Events } from 'ionic-angular';
    
    // first page (publish an event when a user is created)
    constructor(public events: Events) {}
    createUser(user) {
      console.log('User created!')
      this.events.publish('user:created', user, Date.now());
    }
    
    
    // second page (listen for the user created event after function is called)
    constructor(public events: Events) {
      events.subscribe('user:created', (user, time) => {
        // user and time are the same arguments passed in `events.publish(user, time)`
        console.log('Welcome', user, 'at', time);
      });
    }
    

    离子文档:https://ionicframework.com/docs/api/util/Events/

    源码:https://github.com/ionic-team/ionic/tree/master/demos/src/events

    【讨论】:

      【解决方案3】:

      您可以在不使用Events 的情况下处理此问题,而是将回调作为参数传递:

      my-popover.ts

      import { Component, Input } from '@angular/core';
      import { NavParams } from 'ionic-angular';
      
      @Component()
      export class MyPopoverComponent {
      
        @Input()
        text:string = null;
      
        constructor(protected navParams:NavParams) {
          this.text = this.navParams.get("text");
        }
      
        onClicked(event:any) {
          this.navParams.get('clicked')(this.text);
        }
      }
      

      my-popover.html

      <button ion-button (click)="onClicked($event)">{{text}}</button>
      

      my-page.ts

      let popover = this.popoverController.create(MyPopoverComponent, {
        text: "My Popover",
        clicked:(text:string) => {
          console.log(text);
        }
      });
      popover.present();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-03
        • 1970-01-01
        • 1970-01-01
        • 2017-01-17
        • 2018-06-05
        相关资源
        最近更新 更多