2016-09-26 45 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