选择控件上的默认选择

时间:2017-04-13 15:32:38

标签: angular

如何在带有字符串数组的select控件上进行默认选择。

这有效:



            <select class="form-control"
                    [ngModel]="defaultSubscription"
                    name="subscription">                
                <option value="Basic">Basic</option>
                <option value="Advanced">Advanced</option>
                <option value="Pro">Pro</option>
            </select>
&#13;
&#13;
&#13;

但这个不是:

&#13;
&#13;
            <select class="form-control"
                    [ngModel]="defaultSubscription"
                    name="subscription">
                <option *ngFor="let subsciption of subscriptions"
                        value="{{subscription}}">
                    {{subsciption}}
                </option>
            </select>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

使用方括号

let subscriptions = ['subscription1', 'subscription2'];

<select class="form-control"
                [ngModel]="defaultSubscription"
                name="subscription">
            <option *ngFor="let subsciption of subscriptions"
                    [value]="subscription">
                {{subsciption}}
            </option>
</select>

答案 1 :(得分:1)

只需删除value即可按您的意愿工作,此处不需要:

<select class="form-control"
    [ngModel]="defaultSubscription"
    name="subscription">
  <option *ngFor="let subsciption of subscriptions">{{subsciption}}</option>
</select>

Demo