是否可以在Angular中使用两种方式将一个对象的某些属性绑定到另一个对象?一个例子是:
JS
object_1 {
property1:string,
property2:string,
property3:string,
property4:string
}
object_2 {
property1:string,
property2:string,
property3:string
}
objects_1: object_1[];
objects_2: object_2[];
HTML
<ul>
<li *ngFor="let object1 of objects_1">
<input type="text" [(ngModel)]="object1.property1">
<select [(ngModel)]="object1.property2+object1.property3+object1.property4">
<option *ngFor="let object2 of objects_2" [ngValue]="object2">
</option>
</select>
</li>
</ul>
非常感谢您!
答案 0 :(得分:0)
否,这是不可能的,因为ngModel指令的值将被设置为select元素的selected选项(在这种情况下)。因此,这必须是要绑定的单个属性。
但是,如果要将选定选项的值分配给多个属性,则可以使用ngModelChange eventEmitter来填充值:
<select [ngModel]="object1.property2" (ngModelChange)="object1.property2 = $event; object1.property3 = $event; object1.property4 = $event"> ... </select>