简体   繁体   English

在 Ionic 中更改警报 Controller 中的输入类型

[英]Change the Input Type of Inputs in Alert Controller in Ionic

Is the modifying of input type in Alert Controller is possible using the only button inside the prompt itself?是否可以使用提示本身内的唯一按钮修改警报 Controller 中的输入类型?

Problem:问题:

  1. I want to change the input type "password" into "text" when the user press the View Password button.当用户按下查看密码按钮时,我想将输入类型“密码”更改为“文本”。 Do you have any suggestion about that?你对此有什么建议吗?

警报控制器

change password code (profile.ts)更改密码代码 (profile.ts)

 async changePassword(){
    let alert = await this.alertCtrl.create({
      header: 'Change Password',
      subHeader: 'Fill up the fields.',
      inputs: [
        {
          name: 'oldPassword',
          placeholder: 'Old Password.',
          type: 'password'
        },
        {
          name: 'newPassword',
          placeholder: 'New Password.',
          type: 'password',
          value: this.generatePassword(8) //This generate the password
        },
        {
          name: 'newPasswordConfirm',
          placeholder: 'Confirm New Password',
          type: 'password'
        }
      ],
      buttons: [
        {
          text: 'Cancel',
          role: 'cancel',
          handler: data => {
            console.log('Cancel clicked.');
          }
        },
        {
          text: 'View Password',
          handler: data => {
            data.newPassword.type = 'text'; //Error exists
            return false;
          }
        }
      ]
    });
    await alert.present();
  }//

The actual error实际错误

错误

What you asking is not possible with AlertController. AlertController 无法满足您的要求。 It is a very basic component which only returns a json object to you, not the form itself.它是一个非常基本的组件,它只返回一个 json object 给您,而不是表单本身。

For your purposes you should do as Ravi suggested in the comments, use a PopoverController with custom component.出于您的目的,您应该按照 Ravi 在评论中的建议进行操作,使用带有自定义组件的 PopoverController。

You can get the remaining portions from the link above but here are the parts you care about:您可以从上面的链接中获取其余部分,但这里是您关心的部分:

Popovercomponent.page.ts Popovercomponent.page.ts

import { Component } from
'@angular/core';
 import {PopoverController} from '@ionic/angular';
 import { FormBuilder } from '@angular/forms';
 @Component({
   selector: 'app-popovercomponent',
   templateUrl: './popovercomponent.page.html',
   styleUrls: ['./popovercomponent.page.scss'],
 })
 export class PopovercomponentPage {

    form = this.formBuilder.group({
     newPassword: [''],
     oldPassword: [''],
     newPasswordText: [''],
     oldPasswordText: [''],
   });

   showPassword = false;
   doUpdate = false;

   constructor(private popover:PopoverController,
               private formBuilder: FormBuilder) {}

  toggleShowPassword(): void {
    this.showPassword = !this.showPassword;

    if (showPassword) {
      this.form.patchValue({
        oldPasswordText: this.form.oldPassword.value,
        newPasswordText: this.form.newPassword.value
      });
    } else {
      this.form.patchValue({
        oldPassword: this.form.oldPasswordText.value,
        newPassword: this.form.newPasswordText.value
      });
    }

  }

  cancel(): void {
    this.popover.dismiss();
  }

   update(): void {
     this.doUpdate = true;
     this.popover.dismiss();
   }
 } 

Popovercomponent.page.html Popovercomponent.page.html


<ion-content padding>
  <ion-grid>
    <form>
      <ion-row *ngIf="!showPassword">
        <ion-col><input type="password" [formControlName]="oldPassword" placeholder="Old Password"></ion-col>
      </ion-row>
      <ion-row *ngIf="showPassword">
        <ion-col><input type="text" [formControlName]="oldPasswordText" placeholder="Old Password"></ion-col>
      </ion-row>
      <ion-row *ngIf="!showPassword">
        <ion-col><input type="password" [formControlName]="newPassword" placeholder="Confirm new password"></ion-col>
      </ion-row>
      <ion-row *ngIf="showPassword">
        <ion-col><input type="text" [formControlName]="newPasswordText" placeholder="Confirm new password"></ion-col>
      </ion-row>
      <ion-row>
        <ion-col>
          <ion-button color="success" expand="block" (click)="update()">Update</ion-button>
        </ion-col>
      </ion-row>
      <ion-row>
        <ion-col>
          <ion-button color="success" expand="block" (click)="togglePassword()">View Password</ion-button>
        </ion-col>
      </ion-row>
      <ion-row>
        <ion-col>
          <ion-button color="success" expand="block" (click)="cancel()">Cancel</ion-button>
        </ion-col>
      </ion-row>
    </form>
  </ion-grid>
</ion-content> 

Once added to your main code (which is not posted in your question) you will be able to access the data via the popover object.一旦添加到您的主代码(未在您的问题中发布),您将能够通过弹出窗口 object 访问数据。

I would assign an id to each input我会为每个输入分配一个 id

inputs: [
    {
      name: 'oldPassword',
      placeholder: 'Old Password.',
      type: 'password', 
      id: 'new-id'
    },

Then I would change the input type like so然后我会像这样改变输入类型

let passwordInput = document.getElementsByClassName('new-id');
passwordInput.setAttribute('type', 'text')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM