首页 > 解决方案 > 在角度 9 中选择下拉更改传递模式

问题描述

所有人都在基于传递值的下拉更改方法中传递值,只有我做了一些验证以启用提交按钮。而且我还需要比较两个下拉值,在这种情况下我不知道如何在角度 9 中处理它。并且传递的值是位置请找到屏幕截图

 <label for="aCount" class="required">testcount</label>
                <select id="testCounts" class="form-control" formControlName="testcount" #testcount
                    (change)='ontestCountSelected(testcount.value)'>

                    <option *ngFor="let count of testcountsVal" [ngValue]="count.count">
                        {{ count.count }}
                    </option>
                </select>
                <div class="text-danger"
                    *ngIf="submitted && listForm.controls.testcount.hasError('required')">
                    Select count is required
                </div>
            </div>
 public testcountsVal: any[] = [
    { count: '0' },
    { count: '1' },
    { count: '2' },
    { count: '3' },
    { count: '4' }
  ];
        ontestCountSelected(value: string) {
        console.log(value);
         }

所以这里有两个问题:1)一个是我们如何在角度 9 中比较两个不同的下拉模型。 2)为什么当我在下拉列表中传递值 onchange 方法时,在控制台中的位置获得值

请找附件。在此处输入图像描述

标签: angularng-bootstrapangular9angular-bootstrap

解决方案


问题是您设置formControlName与 variable 相同#testcount

您需要更改#testcount#testcount1并用作(change)='ontestCountSelected(testcount1.value)'

<label for="aCount" class="required">testcount</label>
<select id="testCounts" class="form-control" formControlName="testcount" #testcount1
      (change)='ontestCountSelected(testcount1.value)'>

      <option *ngFor="let count of testcountsVal" [ngValue]="count.count">
                        {{ count.count }}
      </option>
 </select>

推荐阅读