2016-09-26 25 views
0

我使用的是Angular2和ngSemantic庫。我創建了一個表格,這裏是我的代碼:ngSemantic select不綁定到對象

模板文件:

<div class="container"> 
    <h1>My first form</h1> 
    <form class="ui form" [formGroup]="myForm"> 
     <sm-loader [complete]="!submitted" class="inverted" text="Loading..."></sm-loader> 

     <div class="field"> 
      <sm-select [(model)]="firstSelect" [control]="myForm.controls.category" placeholder="Search..." class="fluid search"> 
       <option *ngFor="let item of itemslist" [value]="item">{{item.name}}</option> 
      </sm-select> 
     </div> 

     <div class="field"> 
      <sm-select placeholder="Search..." class="fluid search"> 
       <option *ngFor="let i of firstSelect?.items">{{i}}</option> 
      </sm-select> 
     </div> 

     <sm-button (click)="onSubmit()" [disabled]="!myForm.valid" class="primary">Login</sm-button> 
    </form> 
</div> 

組件文件:

itemslist: any = [ 
    { name: 'first', items: ['one', 'two', 'three']}, 
    { name: 'second', items: ['one', 'two', 'three']}, 
    { name: 'third', items: ['one', 'two', 'three']} 
]; 

constructor(private dataService: DataService, fb: FormBuilder) { 
    this.myForm = fb.group({ 
     'category': [], 
     'subcategory': [], 
     'name': [], 
     'email': [] 
    }); 
} 

第二選擇循環不起作用,因爲firstSelect結果是字符串而不是Object。也許發生這種情況是因爲select/option只處理stringnumber

我該如何解決這個問題?

回答

0

{{...}}做字符串插值。如果你想傳遞一個對象實例,不要使用它。

對於對象綁定使用,而不是

[value]="item" 
+0

是的,我知道。我編輯了我的問題,這是一個錯字。對不起,謝謝你的回覆。無論如何,這是行不通的。 – smartmouse

+1

我不知道'sm-select',但對於正常選擇,當綁定對象而不是''元素上的字符串時,您需要使用'[ngValue] =「...」'。 –

+0

我知道。如果在'option'上使用'select'和'ngValue',它會工作:( – smartmouse