显示/隐藏第二个DropDownSelect基于Ionic 3中的First DropDownSelect

时间:2018-03-09 07:06:01

标签: html typescript ionic3 dropdown show-hide

我是离子的新手,我试图根据第一次下拉选择隐藏第二次下拉。 我的第一个DropDown 1.Bank 2.Cash 当我选择Bank Second DropDown时将被隐藏,当选择Cash时,它将显示第二个DropDown。

.html文件

<ion-list no-lines>
  <ion-item>
    <ion-label>Payment Mode</ion-label>
    <ion-select [(ngModel)]="paymentmode" (ionChange)="paymentModeChosen()">
      <ion-option [value]="paymentmode" *ngFor="let paymentmode of 
         paymentmodes()">{{paymentmode}}</ion-option>
     </ion-select>
   </ion-item>

  <ion-item  *ngIf="paymentlocations">
    <ion-label>Payment Location</ion-label>
    <ion-select [(ngModel)]="paymentlocation" 
       (ionChange)="paymentLocationChosen()">
    <ion-option  [value]="paymentlocation" *ngFor="let paymentlocation of 
      paymentlocations()">{{paymentlocation}}</ion-option>
  </ion-select>
</ion-item>
</ion-list>

.ts文件

paymentmodes(): string[] {
return [
  "Bank",
  "Cash"
]; }
 paymentmode: string = "Bank";

 paymentlocations(): string[] {
return [
  "Ahmadabad",
  "Chennai",
  "Delhi",
  "Kolkata",
  "Mahad",
  "Mumbai",
  "Pune",
  "Roha",
  "Sahibabad"
];
}

paymentlocation: string = "Ahmadabad";

constructor(public navCtrl: NavController, public navParams: NavParams) {}

paymentModeChosen(): void {
  console.log(this.paymentmode);}

paymentLocationChosen():void{
 console.log(this.paymentlocation);
}}

2 个答案:

答案 0 :(得分:4)

.ts文件中的更改

在.ts文件中创建新变量

public paymentL = true;

然后修改此功能

paymentModeChosen(): void {
    console.log(this.paymentmode);
    if(this.paymentmode == 'Cash'){
      this.paymentL = false;
    }else{
       this.paymentL = true;
    }

  }

html文件中的更改

在html文件中ngif“paymentL”如果paymentL为“Cash”,则此离子项不可见,如果“Bank”则显示离子项目。

<ion-item  *ngIf="paymentL">
    <ion-label>Payment Location</ion-label>
    <ion-select [(ngModel)]="paymentlocation" 
       (ionChange)="paymentLocationChosen()">
    <ion-option  [value]="paymentlocation" *ngFor="let paymentlocation of 
      paymentlocations()">{{paymentlocation}}</ion-option>
  </ion-select>
</ion-item>

答案 1 :(得分:2)

在.html文件中

   <ion-list no-lines>
  <ion-item>
    <ion-label>Payment Mode</ion-label>
    <ion-select [(ngModel)]="paymentmode" (ionChange)="paymentModeChosen($event)">
      <ion-option [value]="paymentmode" *ngFor="let paymentmode of 
         paymentmodes()">{{paymentmode}}</ion-option>
     </ion-select>
   </ion-item>

  <ion-item  *ngIf="showPaymentLocation"> // decide whether to show or hide
    <ion-label>Payment Location</ion-label>
    <ion-select [(ngModel)]="paymentlocation" 
       (ionChange)="paymentLocationChosen()">
    <ion-option  [value]="paymentlocation" *ngFor="let paymentlocation of 
      paymentlocations()">{{paymentlocation}}</ion-option>
  </ion-select>
</ion-item>
</ion-list>

//在.ts文件中

showPaymentLocation: boolean = false;

constructor(public navCtrl: NavController, public navParams: NavParams) {}

paymentModeChosen(paymentmodeName): void {
  if(paymentmodeName == 'Bank') {
    this.showPaymentLocation = false;
  } else { 
    this.showPaymentLocation = true;
   }
}