2017-04-10 145 views
2

在angular2中面临选择选项的问题。默认情况下,它未被选中并可见。它在IE浏览器中工作,但不在其他浏览器中。Angular2:应该选择“选择”选项,默认情况下是可见的

country.html:

<label class="control-label col-sm-offset-1 col-sm-2 bodySmall" for="country">Country:</label> 
       <div class="col-sm-3" [ngClass]="{ 'has-error': country.errors && (country.dirty || country.touched)}"> 
        <select class="form-control bodySmall" id="country" [(ngModel)]="model.country" name="country" #country="ngModel" required> 
         <!--required--> 

         <option value="" selected="selected">--Please select a Country--</option> 
         <option *ngFor="let c of countries" [value]="c">{{c}}</option> 
        </select> 
        <div *ngIf="country.errors && (country.dirty || country.touched)" class="help-block"> 
         <div [hidden]="!country.errors.required">Country is required.</div> 
        </div> 
       </div> 

FYR Please find screenshot

回答

2

如果你想有一个不可选择项(用户不能手动选择),在默认情况下预选,然后再尝试这样的事:

<option disabled hidden [value]="undefined">Please select a Country</option> 

如果您未填充有效值,则[value]=undefined可以使其预选。禁用和隐藏使选项不能由用户手动选择。

+0

泰勒感谢您的帮助.. –

0

如果要设置默认值应用:

<option disabled selected value>-- Please select a Country --</option> 

如果你已经默认值组件装在上SELECTEDCOUNTRY初始化加:

<option *ngFor="let c of countries" [selected]="selectedCountry == 'c'" >{{c}}</option> 
相关问题