angular2 select 值对象

angular2 select value object

越来越烦人了!我有一个对象数组,我找到了这个解决方案:How to use select/option/NgFor on an array of objects in Angular2

但毕竟我正在做同样的事情但是如果我记录我的 $event 它是未定义的..可能是因为它被字符串化但之后没有被解析回来。

这是我的代码示例:

<div class="container">
<div class="row" *ngFor="#condition of conditions;#conditionindex = index">
    <div class="col-xs-3">
        <select class="form-control" [ngModel]="stringify(condition)" (ngModelChange)="onChange(conditionindex, $event)">
            <option *ngFor="#c of catalog;#catalogindex = index" [value]="stringify(c)">
                {{c.name}}
            </option>
        </select>
    </div>
    <condition-detail [condition]="condition"></condition-detail>
</div>

<a class="btn btn-primary" (click)="newCondition()"><i class="glyphicon glyphicon-plus"></i></a>

这是组件代码:

export class ConditionBuilderComponent implements OnInit {
conditions: Condition[] = [];
catalog: Condition[] = [];

constructor(public _conditionService: ConditionService) { }

getConditions() {
    this._conditionService.getConditions().then(conditions => this.catalog = conditions);
}

ngOnInit() {
    this.getConditions();
}

stringify(o:any): string {
    return JSON.stringify(o);
}

onChange(conditionsIndex, selectedCondition:string): void {
    console.log(typeof selectedCondition);
    //JSON.parse(selectedCondition);
    console.log(selectedCondition);
    //this.conditions[conditionsIndex] = this.catalog[condition];
    console.log(typeof selectedCondition);
}

请帮帮我。 selectedCondition 的控制台日志未定义。

准备就绪:)

这里是解决方案:

<select class="form-control" [ngModel]="condition.name" (ngModelChange)="onChange(conditionindex, $event)">
   <option *ngFor="#c of catalog;#catalogindex = index" [value]="c.name">
                {{c.name}}
   </option>
</select>

和组件文件:

  onChange(conditionsIndex, selectedCondition:string): void {
    this.conditions[conditionsIndex] = this.catalog.find(condition => condition.name == selectedCondition);
}

在最新的 Angular2 版本 (beta 14) 中,添加了对 choose as object 的支持。