有没有办法在表单中使用一个带有多个输入的ngModel?

时间:2017-10-13 18:49:43

标签: angular angular2-ngmodel

请注意,我对Angular来说真的很新。 我的表格包括Year,Make和Model。我试图找到一种方法将所有这些项绑定到一个属性,以生成对象的名称。我不太明白为什么这不会将价值绑定到" vehicle.name"。处理这个问题的正确方法是什么?

<input class="form-control" type="number" value="{{vehicle.year}}" id="vehcile-info" [(ngModel)]="vehicle.year">
<input class="form-control" type="number" value="{{vehicle.year}}" id="vehcile-info" [(ngModel)]="vehicle.year">
<input class="form-control" type="number" value="{{vehicle.year}}" id="vehcile-info" [(ngModel)]="vehicle.year">


<input class="form-control" type="text" readonly value="{{vehicle.year}} {{vehicle.make}} {{vehicle.model}}" id="vehcile-info" [(ngModel)]="vehicle.name">

车辆界面:

export interface Vehicle {
$key: string;
name: string;
year: string;
make: string;
model: string;
trim?: string;
info?: string;
vin?: string;
}

保存车辆功能:

saveVehicle(vehicle: Vehicle) {
    return this
        .vehicles$
        .push(vehicle)
        .then(_ => console.log('success saving vehicle'))
        .catch(error => console.log(error));

0 个答案:

没有答案