选择不使用角度为4的ng-model的选项

时间:2017-06-14 17:17:25

标签: angular

我有一个选择框,我在列表中显示元素 代码段:

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>

我无法看到任何值。列表中只有一个空白字段。任何人都可以指出这里的问题吗?

2 个答案:

答案 0 :(得分:20)

应该是,

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

<强> DEMO

答案 1 :(得分:0)

正确的方法是:

<select id="select-type-basic" [(ngModel)]="status">
    <option *ngFor="let status_item of status_values">
    {{status_item}}
    </option>
</select>

值应避免在选项内部使用,因为这将设置“选择字段”的默认值。默认选择应该与[(ngModel)]绑定,并且应该同样声明选项。

status : any = "Completed";
status_values: any = ["In Progress", "Completed", "Closed"];