2017-12-21 14 views
0
`<select class="form-control" id="customer" required [(ngModel)]="application.customer" name="customer" #customer="ngModel"> 
     <option *ngFor="let customer of customers" [ngValue]="customer"> 
     {{customer.id}} {{customer.first_name}} {{customer.last_name}}</option> 
    </select>` 

application.customerとcustomerはどちらもCustomerオブジェクトのタイプです。コンポーネントにアプリケーションが移入されると、デフォルト値は設定されません。 application.vehicle_makeのような他のテキスト入力フィールドは問題なくロードできます。誰もアプリケーションからのデフォルト値が選択されていない理由を知っていますか?私は最新の角度5を使用しています。角4フォームセレクトはデフォルト値を表示することができます

答えて

1

あなたの特定の1つの用途のIDでそれをバインドしてください!

<select class="form-control" id="customer" required [(ngModel)]="application.customer" name="customer" #customer="ngModel"> <option *ngFor="let customer of customers" [value]="customer.id"> {{customer.id}} {{customer.first_name}} {{customer.last_name}}</option> </select>

は今[ngValue] [値]があり

0

にTRUNは、最も可能性の高いapplication.customerに等しいcustomer of customersではありません。あなたが選択したデフォルトをしたい場合は、アイテムの一つにその属性を追加する必要があります。

<option *ngFor="let customer of customers; let first = first;" [ngValue]="customer" selected="first"> 
    {{customer.id}} {{customer.first_name}} {{customer.last_name}}</option> 

これはcustomersの配列内の最初の項目を選択します。 *ngForlet i = index;を使用して特定のインデックスを選択したり、let last = lastを使用することもできます。これにより、選択されたcustomer of customersと等しいapplication.customerが設定されます。

関連する問題