简体   繁体   English

Angular2自动选择下拉选项(如果有条件)

[英]Angular2 auto select dropdown option if condition

I have an template, where a service populates the values used in a select box. 我有一个模板,其中服务填充选择框中使用的值。 Like so: 像这样:

<div class="form-group">
    <label for="backings_select">Backing</label>
    <select class="form-control"
            required
            name="backings_select"
            (ngModelChange)="storeValueRedux($event, count)">
    <option *ngFor="let backing of backings" [ngValue]="backing.id">{{backing.name}}</option>
    </select>
</div>

The above works fine. 以上工作正常。 So I figured, if the array backings only has one item, I might as well have angular auto select the one value that is available (for UI improvement) 所以我想,如果阵列背衬只有一个项目,我还不如角自动选择一个值可用(UI改进)

So for a basic, hacky proof of concept I tried: 因此,对于基本的概念证明,我尝试过:

<div *ngIf="backings?.length == 1" class="form-group">
    <label for="backings_select">Backing Single</label>
    <select class="form-control"
            required
            name="backings_select"
            (ngModelChange)="storeValueRedux($event, count)">


    <option *ngFor="let backing of backings" [ngValue]="backing.id" selected="selected">{{backing.name}}</option>
    </select>
</div>

<div *ngIf="backings?.length > 1" class="form-group">
    <label for="backings_select">Backing Multiple</label>
    <select class="form-control"
            required
            name="backings_select"
            (ngModelChange)="storeValueRedux($event, count)">
    <option *ngFor="let backing of backings" [ngValue]="backing.id">{{backing.name}}</option>
    </select>
</div>

Now if backings.length == 1 , the following html is rendered: 现在,如果backings.length == 1 ,则呈现以下html:

<option selected="selected" value="1: 1" ng-reflect-ng-value="1">nameValue</option>

And if backings.length > 1 the following html is rendered: 如果backings.length> 1 ,则呈现以下html:

<option value="0: 1" ng-reflect-ng-value="1">nameValue1</option>
<option value="1: 2" ng-reflect-ng-value="2">nameValue2</option>

Now in theory, the above code should work, however when length==1 the html select box is not auto selecting the value with selected="selected". 现在从理论上讲,上面的代码应该可以工作,但是当length == 1时,html select框不会自动使用selected =“ selected”选择值。 Have I missed anything, or is there any reason why the select is not auto selecting? 我错过了什么吗?或者为什么没有自动选择?

use [selected] instead of selected 使用[selected]而不是selected

<option *ngFor="let backing of backings" [ngValue]="backing.id" [selected]="backings.length === 1">{{backing.name}}</option>

without using ngIf and duplicating your code you can achieve the desired result like this: 无需使用ngIf并复制代码,就可以实现所需的结果,如下所示:

<div class="form-group">
    <label for="backings_select">Backing</label>
    <select class="form-control"
            required
            name="backings_select"
            (ngModelChange)="storeValueRedux($event, count)">
         <option *ngFor="let backing of backings" [ngValue]="backing.id" [selected]="backings.length === 1">{{backing.name}}</option>
{{backing.name}}</option>
    </select>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM