代码之家  ›  专栏  ›  技术社区  ›  SmartestVEGA

下拉列表中的默认选定值不起作用

  •  -2
  • SmartestVEGA  · 技术社区  · 6 年前

    我有一个下拉列表,默认情况下在页面加载时,在下拉列表中选择“请选择用例”。

    但是我希望在页面加载时选择“euc代理”,而不是“请选择用例”。

    html is shown below.

    <div class=“form group”>
    <label for=“bot”>用例</label>
    <select id=“select”formcontrolname=“UseCase”class=“Form Control”class=“Form Control”[(ngModel)]=“调度模式.useCaseID”>
    <option value=“-1”[选中]=“IsSelected”>请选择用例</option>
    <option*ngfor=“let uc of usecaseList”[值]=“uc.botid”>uc.botname
    </选项>
    </选择>
    </DIV>
    </DIV>
    

    *html i changed to*.

    [选中]=“1”

    但这没有任何区别。请参阅下面更改的HTML

    <div class=“form group”>
    <label for=“bot”>用例</label>
    <select id=“select”formcontrolname=“UseCase”class=“Form Control”class=“Form Control”[(ngModel)]=“调度模式.useCaseID”>
    <option*ngfor=“let uc of usecaseList”[所选]=“1”[值]=“uc.botid”>uc.botname
    </选项>
    </选择>
    </DIV>
    </DIV>
    

    图像中相同

    我正在下拉列表中选择“测试bot”,这是下拉列表中的最后一项,如下所示:

    但我期待这样的结果:uc.botid=1是“euc代理”而不是“test bot”

    ts file

    ngoninit()。{
    getUseCaseList()。{
    this.managebotservice.getallbots().subscribe(res=>this.usecaselist=res);
    
    }
    }
    

    为什么我无法选择页面加载时uc.botid=1的“euc代理”?

    但是我希望在页面加载时选择“euc代理”,而不是“请选择用例”

    enter image description here

    HTML如下所示

    <div class="form-group">
                                <label for="bot">Use Case</label>
                                <select id="select" formControlName="useCase" class="form-control" class="form-control" [(ngModel)]="scheduleModel.UsecaseId">
                                    <option value="-1" [selected]="isSelected"> Please Select Use Case </option>
                                    <option *ngFor="let uc of useCaseList"  [value]="uc.BOTId"> {{uc.BOTName}}
                                    </option>
                                </select>
                            </div>
                        </div>
    

    *HTML我改为*

    [选中]=“1”

    但这没有任何区别。请参阅下面更改的HTML

     <div class="form-group">
                                    <label for="bot">Use Case</label>
                                    <select id="select" formControlName="useCase" class="form-control" class="form-control" [(ngModel)]="scheduleModel.UsecaseId">
                                                                                <option *ngFor="let uc of useCaseList" [selected]="1" [value]="uc.BOTId"> {{uc.BOTName}}
                                        </option>
                                    </select>
                                </div>
                            </div>
    

    图像相同 enter image description here

    我将在下拉列表中选择“测试bot”,这是下拉列表中的最后一项,如下所示:

    enter image description here

    但我希望这样:其中uc.botid=1是“euc代理”而不是“test bot”

    enter image description here

    TS文件

     ngOnInit() {
      getUsecaseList() {
          this.manageBotService.getAllBots().subscribe(res => this.useCaseList = res);
    
      }
    }
    

    为什么我无法选择页面加载时uc.botid=1的“euc代理”?

    3 回复  |  直到 6 年前
        1
  •  1
  •   Bert Maurau    6 年前

    从选项中删除[选定]。

    “select”中的[(ngmodel)]部分将值设置为selected(取决于“scheduleModel.useCaseID”的值)。

        2
  •  1
  •   Vin Aeoua    6 年前

    我没有看到您使用使其成为默认值的指令:

    value='default' 
    

    例如:

    <select name='test'>
    <option value='default' selected='selected'>Please select the value</option>
    <option value='value1'>value1</option>
    <option value='value2'>value2</option>
    </select>
    
        3
  •  1
  •   Ramesh Rajendran    6 年前

    集合 scheduleModel.UsecaseId = 1 加载下拉列表后 ng-init() .