2016-09-17 49 views
2

你好,我刚刚跑到一个问题,当我尝试编辑我有一个下拉列表,并没有显示任何值,好像它不想绑定...这里是我的代码和结果:为什么选择选项的值不绑定时编辑实体角2

查看:

<select class="form-control" id="selectedCategory" 
    required 
    [(ngModel)]="subCategory.category" name="selectedCategory"> 
    <option *ngFor="let category of categories" [ngValue]="category">{{category.name}}</option> 
</select>` 

这里是我的实体:

category.ts类:

import { ICategory } from "../interfaces/icategory"; 

export class Category implements ICategory { 
    id: number; 
    name: string; 
    image: string; 
} 

subcagory.ts类:

import { ISubCategory } from "../interfaces/isubcategory"; 
import { ICategory } from "../interfaces/icategory"; 

export class SubCategory implements ISubCategory { 
    id: number; 
    name: string; 
    image: string; 
    category: ICategory; 
} 

这里是结果我可以在开发者控制台通过调试得到:

enter image description here

,如果我去的形式存在空DDL,但有是所有类别的值...

enter image description here

有没有人知道我出错的地方? :) 谢谢你的帮助。

回答

1

[(ngModel)]="subCategory.category"是指有可能成为同一实例<option *ngFor="let category of categories"

使用的值,如果它只是具有相同内容的对象,这是不是足够多。什么是对象身份,而不是内容。

+0

但它不一样?这些字段是相同的... – Marius

+0

不确定你的意思。如果不一样,那么默认情况下就没有办法让它被选中。您可以使用默认值通过比较其“属性”在“类别”中查找正确的实例,然后将找到的实例设置为默认值。这样可以确保它是完全相同的实例。 –