2017-12-30 8 views
0

잘 작동하는 ngFor을 사용하여 양방향 바인드 select 드롭 다운을 만들고 있는데, 페이지가로드되지 않으면 option이 선택되지 않습니다. [selected]으로 시도했지만 다시는 아무 일도 일어나지 않습니다.양방향 바인드 선택 드롭 다운에서 옵션을 선택하지 않았습니까?


코드 :

<ng-container *ngFor="let attribute of details.Attributes"> 
    <tr *ngIf="attribute.IsConfigurator == 'true'"> 
     <td align="right">{{attribute.PropertyName}}</td> 
     <td> 
      <select [(ngModel)]="config[attribute.Pid]" (ngModelChange)="updateConfig()" class="form-control"> 
       <option *ngFor="let values of attribute.Values; let i = index" value="{{values.Vid}}" [selected]="i == 0">{{values.Value}}</option> 
      </select> 
     </td> 
    </tr> 
</ng-container> 

답변

1
ngOnInit

selected : any; 
ngOnInit() { 
    this.selected = this.attribute.Values[0]; 
} 

에 구성 요소 내부의 ngModel 값을 설정

내가이 있기 때문에 다음

<select [(ngModel)]="selected " (ngModelChange)="updateConfig()" class="form-control"> 
    <option *ngFor="let values of attribute.Values; let i = index" value="{{values.Vid}}">{{values.Value}}</option> 
</select> 
+0

내 페이지에 10 개 이상의'select' 드롭 다운이 있기 때문에'selected'를'ngModel'로 사용할 수 없습니다 ... –

+1

다른 방법을 사용하더라도 10 개의 다른 변수가 필요합니다. – Sajeetharan

1

[selected]="i == 0"는 어떤 표현이 ngModel

values = ['a', 'b', 'c']; 
selected = 'b'; 
에 결합 당신이 config[attribute.Pid]에 선택한 원하는 값 또는 지정 [(ngModel)]="config[attribute.Pid]"

작동하지 않습니다

<select [(ngModel)]="selected"> 
    <option *ngFor="let item of values" [value]="item">{{item}}</option> 
</select> 

은 처음에 b이 선택됩니다.

+0

나는, ngModel''로'selected'을 사용할 수 없습니다 10+ 내 페이지의 'select'드롭 다운을 모두 ... –

+1

그런 다음'selected1' ~'selected10' 또는 배열을 사용하십시오. 'selected'는 단순한 예제 일뿐입니다. '