2017-12-21 36 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는 모두 고객 개체 유형입니다. 응용 프로그램이 구성 요소에 채워지면 기본값이 설정되지 않습니다. application.vehicle_make와 같은 다른 텍스트 입력 필드는 문제없이로드 될 수 있습니다. 누구든지 응용 프로그램의 기본값을 선택하지 않은 이유를 알고 있습니까? 나는 최신 각을 사용하고있다.각도 4 폼 선택은 기본값을 표시 할 수 있습니다.

답변

1

당신의 특별한 용도의 이드로 묶어 라!

<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

을 켜세요 가장 가능성이 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의 배열에서 첫 번째 항목을 선택합니다. *ngFor에서 let i = index;을 사용하여 특정 색인을 선택하거나 let last = last을 사용할 수도 있습니다. application.customer을 선택한 customer of customers과 동일하게 설정합니다.