离子2 - 项目滑块 - 滑动到触发功能

Han*_*Che 1 typescript ionic-framework ionic2 ionic3 angular

我有带3个按钮的ionic-item-sliding指令.2(A和B)向左滑动时出现,右侧滑动1(按钮C).

我想实现当我真正将项目拖到最右边时触发按钮C下的函数的行为.

ionic2 hompage上的文档以此代码片段为例

ondrag(event, item) {
  let percent = event.getSlidingPercent();
  if (percent > 0) {
    // positive
    console.log('right side');
  } else {
    // negative
    console.log('left side');
  }
  if (Math.abs(percent) > 1) {
        this.navCtrl.push(NextPage,{param:item},{ animate: true, direction: 'forward' })
  }
}
Run Code Online (Sandbox Code Playgroud)

使用它

<ion-item-sliding *ngFor="let item of items "(ionDrag)="ondrag($event, item)"></ion-item-sliding>
Run Code Online (Sandbox Code Playgroud)

我得到的行为是,当我滑动太远时,它会多次调用并推送页面(我猜多少事件和我一样)

有关如何正确实现这一点的任何建议?

谢谢!

seb*_*ras 5

您可以添加一个标志,以确保该push()方法只执行一次.

import { Component } from "@angular/core";
import { NavController } from 'ionic-angular/index';
import { Page1 } from 'page1.ts';

@Component({
     templateUrl:"home.html"
})
export class HomePage {

  private alreadyPushed: boolean;

  private items: Array<any>;

  private selectedItem: any;

  constructor(private navCtrl: NavController) {
    this.items = [
      'City of Amsterdam',
      'City of Bogota',
      'City of Buenos Aires',
      'City of Dhaka'
    ];
  }

  ionViewDidEnter() {
    // Initialize the flag
    this.alreadyPushed = false;

    if(this.selectedItem) {
      // Reset the selected item
      this.selectedItem._setOpenAmount(0);
    }

  }

  ondrag(event, item) {
    let percent = event.getSlidingPercent();
    if (percent > 1 && !this.alreadyPushed) {

      // Store the event to reset it later
      this.selectedItem = event;

      // Set the flag to true
      this.alreadyPushed = true;

      // Push the new page
      this.navCtrl.push(Page1, { param : item });
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

另请注意,您应该使用percent > 1而不是Math.abs(percent) > 1这样,只有在向右滑动时才推动页面.