2017-06-14 66 views
0

NG-模式工作,我有一個選擇框,我展示元素從列表 代碼段:選擇選項不與角4

export class CreateauctionComponent implements OnInit{ 

    createAuctionForm: FormGroup; 
    test:any = ["cat","dog"]; 

    constructor(private _formBuilder: FormBuilder,private _categories: UserCategoriesForAuctionService){ 
     // 
    } 
} 

在HTML渲染爲:

<div class="form-col"> 
    <h5><span style="color:red;">*</span>{{'createAuction.category' | translate}}:</h5> 
    <select class="form_cbx long-select" name="" ng-model="test"> 
     <option ng-options="t in test">{{t}}</option> 
    </select> 
</div> 

我無法看到任何值。列表中只有一個空白字段。任何人都可以指出這裏有什麼問題?

+1

你的HTML看起來像角1,沒有棱角4. – brunobastosg

回答

8

它應該是,

<select [(ngModel)]="selectedanimal" (ngModelChange)="onChange($event)"> 
    <option *ngFor="let c of test" [ngValue]="c"> {{c}} </option> 
</select> 

DEMO

+0

得到這個錯誤:試圖區分'貓'錯誤。僅允許數組和迭代器 –

+0

它工作。我的ngmodel的名稱與數組相同 –

1

您需要在select標籤使用compareWith屬性,但如果您使用的角度4,你的HTML仍然看起來像使用angularJs。

HTML文件:

<select [compareWith]="byAnimal" [(ngModel)]="selectedAnimal"> 
    <option *ngFor="let animal of animals" [ngValue]="animal"> 
    {{animal.type}} 
    </option> 
</select> 

TS文件

byAnimal(item1,item2){ 
    return item1.type == item2.type; 
} 

一個最好的soltion從這個link