【问题标题】:Radio group not working properly when changing the radio group list values frequently in ionic 4在 ionic 4 中频繁更改无线电组列表值时,无线电组无法正常工作
【发布时间】:2019-12-12 23:16:45
【问题描述】:

Ionic4 无线电组在动态更改无线电组列表值时行为不端。

让我们以我的用例为例,我有一个字段国家(离子选择),其中包含国家列表。另一个名为 state (ion-radio-group) 的字段将显示基于国家选择的值。根据国家/地区选择,州将显示良好,但问题是当我单击无线电组时,它不会在 UI 中做出反应。当我怀疑单击时,选择将反映在单选组中。为什么它没有反映在我的第一次点击中。请指导我。

homepage.html

<ion-header>
  <ion-toolbar color="primary">
    <ion-buttons slot="start">
      <ion-menu-button></ion-menu-button>
    </ion-buttons>
    <ion-title>
      Home
    </ion-title>
  </ion-toolbar>
</ion-header>
<ion-content>
  <ion-item>
    <ion-label>Country</ion-label>
    <ion-select interface="popover" [(ngModel)]="selectedCountry['result']" (ionChange)="changeCountryCallback()">
      <ion-select-option *ngFor="let country of pickListValues['country']" value="{{country.choiceValue}}"
        [selected]=false>
        {{country.choice}}
      </ion-select-option>
    </ion-select>
  </ion-item>

  <ion-item *ngIf="selectedCountry['result'] != ''">
    <ion-row>
      <ion-label>State</ion-label>
    </ion-row>
    <ion-row>
      <ion-list lines="none">
        <ion-radio-group (ionChange)="changeStateCallback()" [(ngModel)]="selectedState">
          <ion-item *ngFor="let state of selectedCountry['state']">
            <ion-label>{{state.choice}}</ion-label>
            <ion-radio slot="start" value="{{state.choiceValue}}" [checked]="false"></ion-radio>
          </ion-item>
        </ion-radio-group>
      </ion-list>
    </ion-row>
  </ion-item>
</ion-content>

homepage.ts

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

@Component({
  selector: 'app-homepage',
  templateUrl: './homepage.html',
  styleUrls: ['./homepage.scss'],
})
export class homepage implements OnInit {
  private pickListValues = {
    country: [{
      choiceValue: "in",
      choice: "India",
      child: {
        value: [{
          choiceValue: "tn",
          choice: "Tamilnadu",
          child: {
            value: []
          }
        }, {
          choiceValue: "dl",
          choice: "Delhi",
          child: {
            value: []
          }
        }, {
          choiceValue: "mi",
          choice: "Mumbai",
          child: {
            value: []
          }
        }]
      }
    }, {
      choiceValue: "ci",
      choice: "China",
      child: {
        value: [{
          choiceValue: "AH",
          choice: "Anhui Province",
          child: {
            value: []
          }
        }, {
          choiceValue: "GX",
          choice: "Guangxi Zhuang",
          child: {
            value: []
          }
        }]
      }
    }, {
      choiceValue: "jp",
      choice: "Japan",
      child: {
        value: [{
          choiceValue: "gu",
          choice: "Gunma",
          child: {
            value: []
          }
        }, {
          choiceValue: "kw",
          choice: "Kanagawa",
          child: {
            value: []
          }
        }, {
          choiceValue: "oki",
          choice: "Okinawa",
          child: {
            value: []
          }
        }, {
          choiceValue: "tok",
          choice: "Tokushima",
          child: {
            value: []
          }
        }]
      }
    }]
  }
  selectedCountry = {
    result: "",
    state: []
  }
  selectedState: any;
  constructor() {
  }

  ngOnInit() {
  }

  changeCountryCallback() {
    console.log("Country Value : ", this.selectedCountry['result']);
    this.pickListValues['country'].forEach(element => {
      console.log("Element : ", element);
      if (element['choiceValue'] == this.selectedCountry['result']) {
        this.selectedCountry['state'] = [];
        this.selectedCountry['state'] = element['child']['value'];
      }
    })
  }
  changeStateCallback() {
    console.log("State Value : ", this.selectedState);
  }
}

我的预期行为是,在频繁更改国家/地区值后,只需单击一下无线电组即可正常工作

请观看视频的结尾,您可以理解我的问题。点击here观看视频

【问题讨论】:

    标签: ionic-framework ionic4 radio-group


    【解决方案1】:
    <ion-content>
        <ion-item>
          <ion-label>Country</ion-label>
          <ion-select interface="popover" [(ngModel)]="selectedCountry['result']" (ionChange)="changeCountryCallback()">
            <ion-select-option *ngFor="let country of pickListValues['country']" value="{{country.choiceValue}}" [selected]=false>
              {{country.choice}}
            </ion-select-option>
          </ion-select>
        </ion-item>
    
        <ion-item *ngIf="selectedCountry['result'] != ''">
          <ion-row>
            <ion-label>State</ion-label>
          </ion-row>
          <ion-row>
            <ion-list lines="none">
              <ion-radio-group (ionChange)="changeStateCallback()" [(ngModel)]="selectedState">
                <ion-item *ngFor="let state of selectedCountry['state']">
                  <ion-label>{{state.choice}}</ion-label>
                  <ion-radio slot="start" value="{{state.choiceValue}}" [checked]="selectedState==state.choiceValue"></ion-radio>
                </ion-item>
              </ion-radio-group>
            </ion-list>
          </ion-row>
        </ion-item>
      </ion-content>
    

    您可以通过上述方式完成([checked]="selectedState==state.choiceValue")。在这里,我检查了从传递的数组中选择了哪一个。所以在第一次点击时,它应该工作正常。请尝试并分享反馈。

    【讨论】:

      【解决方案2】:
        <div>
            <ion-label class="heading">Gender</ion-label>
            <div *ngFor='let gend of gen'>\\ gen have my details
      
              <ion-radio-group [(ngModel)]="register.Gender" [ngModelOptions]="{standalone: true}"> 
                <ion-row style='margin-top:10px'>
                  <ion-col size='10'>
                    <ion-label style='color: #b4b4b4;margin:0 0 0 40px'> {{gend.name}}</ion-label>
                  </ion-col>
                  <ion-col size='2'>
                    <ion-radio [value]="gend.name"></ion-radio>\\value bind to ngModel
                  </ion-col>
                </ion-row>
              </ion-radio-group>
      
            </div>
          </div>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-25
        • 2021-01-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多