简体   繁体   English

绑定选择/选项 Angular 8

[英]Binding select/options Angular 8

StackBlitz: StackBlitz:

https://stackblitz.com/edit/angular-mezz1b https://stackblitz.com/edit/angular-mezz1b

I have this model:我有这个 model:

import { Users } from './Users';
import { Puntos } from './Puntos';
import { Empleados } from './Empleados';

export class PuntosEmpleados {
    Id?: number;
    Fecha?: string;
    Puntos?: Puntos;
    User?: Users;
    Empleado?: Empleados;
}

My PuntosEmpleados.TS:我的 PuntosEmpleados.TS:

 PuntoEmpleado: PuntosEmpleados = {};

 Puntos: Puntos[] = [];
 Empleados: Empleados[] = [];
 Usuarios: Users[] = [];

My PuntosEmpleados.HTML: (the same with Puntos and Usuarios)我的 PuntosEmpleados.HTML:(与 Puntos 和 Usuarios 相同)

<div class="form-group col-md-6">
    <label>Empleado: </label>
    <select class="form-control form-control-sm" name="empleado"
        [(ngModel)]="PuntoEmpleado.Empleado" >
        <option *ngFor="let emp of Empleados" [ngValue]="emp">
            {{ emp.Nombre }} {{ emp.Apellido }}
        </option>
    </select>
</div>

My problem is when i want to edit one employee(Empleado).我的问题是当我想编辑一名员工(Empleado)时。 Passed the employee to PuntoEmpleado Object, but in the selects doesn´t show the descriptions(Nombre, Apellido, etc..)将员工传递给 PuntoEmpleado Object,但在选择中不显示描述(Nombre、Apellido 等)

I try to define de model in the component.ts and it works fine, but i don´t want that.我尝试在 component.ts 中定义 de model 并且它工作正常,但我不想要那样。

I looked for some similar queries but none with the same error.我寻找了一些类似的查询,但没有一个有相同的错误。

Thanks in advance.提前致谢。

Is the Empleado property from the PuntoEmpleado object an instance of an object in the Empleados array? PuntoEmpleado object 中的 Empleado 属性是 Empleados 数组中 object 的实例吗? ngValue and ngModel are only going to match an object if they are the same instance, not objects with the same values or value type objects like strings or numbers. ngValue 和 ngModel 只会匹配 object 如果它们是相同的实例,而不是具有相同值的对象或值类型对象(如字符串或数字)。

Just like how these two identical objects are not equal, ngModel wont match two objects that are not the same instance.就像这两个相同的对象不相等一样,ngModel 不会匹配两个不同实例的对象。

 console.log({ prop: '1' } === { prop1: '1' });

You can either use a value property in the PuntoEmpleado object like id, they will match or you can look up the instance of the object from the array and assign it to the Empleado property.您可以使用 PuntoEmpleado object 中的值属性(如 id),它们将匹配,或者您可以从数组中查找 object 的实例并将其分配给 Empleado 属性。

Here is an edit of your StackBlitz where I have put the Empleado from the PuntoEmpleado in the Empleados array.这是您的 StackBlitz 的编辑,我将来自 PuntoEmpleado 的 Empleado 放入 Empleados 数组中。 It works because it is the same reference of the object.它之所以有效,是因为它与 object 的引用相同。 https://stackblitz.com/edit/angular-rhrzhf https://stackblitz.com/edit/angular-rhrzhf

My strategy would be to have an EmpleadoId property on the PuntosEmpleados object and bind to that.我的策略是在 PuntosEmpleados object 上拥有一个 EmpleadoId 属性并绑定到该属性。

<div class="form-group col-md-6">
    <label>Empleado: </label>
    <select class="form-control form-control-sm" name="empleadoId"
        [(ngModel)]="PuntoEmpleado.EmpleadoId" >
        <option *ngFor="let emp of Empleados" [ngValue]="emp.Id">
            {{ emp.Nombre }} {{ emp.Apellido }}
        </option>
    </select>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM