【问题标题】:IonPicker Showing wrong valuesIonPicker 显示错误的值
【发布时间】:2019-11-16 13:21:48
【问题描述】:

我正在制作一个显示选项数组的选定文本的 Ionic 应用程序,我正在使用 ion-picker 组件(互联网上没有那么多文档),当我 console.log 值时一切正常索引的它在控制台中显示正确的值,我也在使用警报将所选值显示为消息的一部分,但是当我选择例如第一个值时,警报没有显示任何值,那么如果我选择第二个值警报显示第一个值,我不明白这是怎么回事,因为 console.log 具有正确的值。

这是我的两个功能。

async selectHora() {
    const times: PickerOptions = {
      cssClass: 'horasSelect',
      buttons: [
        {
          text: 'Cancelar',
          role: 'cancel',
          cssClass: 'btnCancel'
        },
        {
          text: 'Confirmar',
          handler: (value) => {
            value = this.horaSeleccionada;
            console.log('confirmacion', this.horaSeleccionada);
            this.confirmacion(value);
          }
        }
      ],
      columns: [
        {
          name: 'Horas',
          options: [
            {text: '8:00 - 9:00', value: 1},
            {text: '9:00 - 10:00', value: 2},
            {text: '10:00 - 11:00', value: 3},
            {text: '11:00 - 12:00', value: 4},
            {text: '12:00 - 13:00', value: 5},
            {text: '13:00 - 14:00', value: 6},
            {text: '14:00 - 15:00', value: 7},
            {text: '15:00 - 16:00', value: 8},
            {text: '16:00 - 17:00', value: 9},
            {text: '17:00 - 18:00', value: 10}
          ]
        }
      ]
    };
    const picker = await this.picker.create(times);
    picker.present();
    picker.onDidDismiss().then(async data => {
      const col = await picker.getColumn('Horas');
      console.log('Esta es la columna ', col);
      this.horaSeleccionada = col.options[col.selectedIndex].value;
      console.log(this.horaSeleccionada);
      // this.confirmacion();
    });
  }

async confirmacion(value) {
    const alert = await this.alertCtrl.create({
      header: 'Confirmación Pedido',
      subHeader: 'Si desea cambiar la hora, seleccione cancelar',
        message: 'Su pedido será entregado entre las ' + value,
        buttons: [
          {
            text: 'OK',
            handler: () => {
              console.log('ok texto', this.horaSeleccionada);
              // this.router.navigateByUrl('/orderfinished');
            }
          }
        ],
    });

    await alert.present();
  }

【问题讨论】:

    标签: angular ionic-framework ionic4


    【解决方案1】:

    看来你分配变量的方式错误value = this.horaSeleccionada;错误this.horaSeleccionada = value正确你能检查它并让我知道它是否工作

    【讨论】:

      【解决方案2】:

      您好,您的问题已解决

      stackblitz.com

      错误

      value = this.horaSeleccionada;
      

      正确方法

      this.horaSeleccionada =value;
      

      工作示例

      import { Component } from '@angular/core';
      
      import { Platform } from '@ionic/angular';
      import { SplashScreen } from '@ionic-native/splash-screen/ngx';
      import { StatusBar } from '@ionic-native/status-bar/ngx';
      import { AlertController } from '@ionic/angular';
      
      import { PickerController } from '@ionic/angular';
      
      export interface PickerOptions {
        buttons?: any[];
        columns?: any[];
        cssClass?: string;
        mode?: string;
        enableBackdropDismiss?: boolean;
      }
      
      @Component({
        selector: 'app-root',
        templateUrl: 'app.component.html'
      })
      
      export class AppComponent {
         horaSeleccionada:any;
        constructor(
          private platform: Platform,
          private splashScreen: SplashScreen,
          private statusBar: StatusBar,
          private alertCtrl:AlertController,
          private picker:PickerController
        ) {
          this.initializeApp();
        }
      
        initializeApp() {
          this.platform.ready().then(() => {
            this.statusBar.styleDefault();
            this.splashScreen.hide();
            this.selectHora(); 
          });
        }
      
        async selectHora() {
      
          const times: PickerOptions = {
            cssClass: 'horasSelect',
            buttons: [
              {
                text: 'Cancelar',
                role: 'cancel',
                cssClass: 'btnCancel'
              },
              {
                text: 'Confirmar',
                handler: (value) => {
                   this.horaSeleccionada=value;
                  console.log('confirmacion',  this.horaSeleccionada);
                  this.confirmacion(value);
                }
              }
            ],
            columns: [
              {
                name: 'Horas',
                options: [
                  {text: '8:00 - 9:00', value: 1},
                  {text: '9:00 - 10:00', value: 2},
                  {text: '10:00 - 11:00', value: 3},
                  {text: '11:00 - 12:00', value: 4},
                  {text: '12:00 - 13:00', value: 5},
                  {text: '13:00 - 14:00', value: 6},
                  {text: '14:00 - 15:00', value: 7},
                  {text: '15:00 - 16:00', value: 8},
                  {text: '16:00 - 17:00', value: 9},
                  {text: '17:00 - 18:00', value: 10}
                ]
              }
            ]
          };
          const picker = await this.picker.create(times);
          picker.present();
          picker.onDidDismiss().then(async data => {
            const col = await picker.getColumn('Horas');
            console.log('Esta es la columna ', col);
            this.horaSeleccionada = col.options[col.selectedIndex].value;
            console.log(this.horaSeleccionada);
            // this.confirmacion();
          });
        }
      
      async confirmacion(value) {
          const alert = await this.alertCtrl.create({
            header: 'Confirmación Pedido',
            subHeader: 'Si desea cambiar la hora, seleccione cancelar',
              message: 'Su pedido será entregado entre las ' + value,
              buttons: [
                {
                  text: 'OK',
                  handler: () => {
                    console.log('ok texto', this.horaSeleccionada);
                    // this.router.navigateByUrl('/orderfinished');
                  }
                }
              ],
          });
      }
      }
      

      【讨论】:

      • 感谢您的回答,我现在得到了结果,但现在结果是 [object Object],我尝试将其转换为 JSON 对象,但没有回答。我尝试将索引保存在另一个变量上,但结果显示“未定义”
      • 只需添加console.log(JSON.stringify(this.horaSeleccionada));-------JSON.stringify
      • 这很好用,我自己找到了另一个解决方案,结果是 Ok 和 Confirm 按钮的处理程序,接收到一个事件,所以只要有这个事件就足以让它显示正确的值,谢谢你的回答
      猜你喜欢
      • 1970-01-01
      • 2020-06-19
      • 2021-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-31
      相关资源
      最近更新 更多