微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

FormArray动态下拉列表数据未正确加载

如何解决FormArray动态下拉列表数据未正确加载

无法使用FormArray索引加载选定的国家/地区数据。在传递国家/地区代码获取数据时,它会继续调用API。

尝试一下,

<form [formGroup]='formName'>
              <div formArrayName="controllerArray" class="ui-g ui-g-12 ui-g-nopad " >
                <div  class="ui-g  ui-g-12 ui-g-nopad " *ngFor="let item of formName.controls.controllerArray.controls; let i=index" [formGroupName]="i">  
                  <div class="ui-md-4">                   
                    <label class="mandatory"
                  >{{ labels["STATE_OF_INCORPORATION_FILE_NUMBER"] }}
                  <span class="colon"></span>
                </label>           

                <select formControlName="select1" class="ui-inputtext"> 
                  <option>--Select--</option>
                  <option *ngFor="let c of countryOptions" [ngValue]="c.value">{{c.label}}</option>
                 
                </select>
              </div>
              <div class="ui-md-4">
                <label class="lbl-hidden"> State </label>
                <select formControlName="select2" class="ui-inputtext"> 
                  <option>--Select--</option>
                  <option *ngFor="let b of getRelevantStates(item.controls.select1.value)" [ngValue]="b.value">{{b.label}}</option>
                 
                </select>
              </div>
              <div class="ui-md-3">
                <label class="lbl-hidden"> comments  </label>
                <input
                  type="text"
                  pInputText
                  class="form-control"
                  formControlName="input" />
                </div>
                <div class="ui-md-1">
                  <label class="lbl-hidden"> State </label>
                  <br/>
                  <button (click)='removeInput(i)'  style="min-width: auto;"  pButton icon="fa fa-minus"></button>
                </div>
                </div>
              </div>
            </form>
            
            <button class="add-remove-btn" pButton (click)='addinput()' icon="fa fa-plus"></button>

TS

getRelevantStates(countryId){
var states = [];
   this.serviceName.getStates(countryId).subscribe((res) => {
     return res[0].results;
    }

}

问题

enter image description here

预期为

应在每行中根据所选国家/地区加载州/地区。

enter image description here

解决方法

我无法重现您的脚本并检查其工作方式,但我认为将足够的异步管道添加到您的html中:

<option *ngFor="let b of (getRelevantStates(item.controls.select1.value) | async)" [ngValue]="b.value">{{b.label}}</option>
,

您必须将该函数调用从html移至ngOnInit或任何其他生命周期事件,并使用变量来存储数据,

 *ngFor="let item of collection"

定期对组件进行角度调用更改检测。如果要使用当前方法,请使用更改检测策略。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。