简体   繁体   English

Angular2反应形式下拉菜单

[英]Angular2 reactive forms dropdown

I am just learning angular 2 and have a case where I am creating a form that will be a piece of equipment. 我只是在学习angular 2,并且有一种情况,我正在创建一个将成为一件设备的表格。 One of the fields that are associated with the equipment is a building. 与设备关联的领域之一是建筑物。 When the user is editing or adding a new piece of equipment I want them to be presented with a drop-down with a list of the building they can assign the equipment to. 当用户编辑或添加新设备时,我希望向他们显示一个下拉列表,其中包含可以分配设备的建筑物列表。 The equipment-detail component is as follows: 设备详细信息组件如下:

  export class EquipmentDetailComponent implements OnInit {
  equipment: IEquipment;
  equipmentForm: FormGroup;
  buildingList: Ibuilding[];

  constructor(private EquipmentService: EquipmentService,
              private BuildingsService: BuildingsService) { }

  ngOnInit() {
    this.equipment = this.EquipmentService.getEquipmentDetail(1);
    this.buildingList = this.BuildingsService.getBuildingList();

    let id = new FormControl(this.equipment.id);
    let unit = new FormControl(this.equipment.unit);
    let active = new FormControl(this.equipment.active);
    let building = new FormControl(this.equipment.building.id);

    this.equipmentForm = new FormGroup({
      id: id,
      unit: unit,
      active: active,
      building: building
    })    
  }

  saveEquipment(formValues){
    console.log(formValues);
  }

  onSelect(id){
    this.equipment.building = this.BuildingsService.getBuildingDetail(id);
    console.log(this.equipment.building)
  }
}

The equipment-detail html is as follows: equipment-detail html如下:

<div class="col-md-2">
  <form [formGroup]="equipmentForm" (ngSubmit)="saveEquipment(equipmentForm.value)" autocomplete="off" novalidate>
    <div class="form-group">
      <label form="unit">Unit:</label>
      <input formControlName="unit" id="unit" type="text" class="form-control" placeholder="Unit...."/>
    </div>
    <div class="form-group">
      <label form="active">Active:</label>
      <input formControlName="active" id="active" type="checkbox" class="form-control"/>
    </div>
    <div class="form-group">
      <label form="building">Building:</label>
      <select class="form-control" formControlName="building" (change)="onSelect($event.target.value)">
        <option *ngFor="let building of buildingList" value={{building.id}}>
          {{building.buildingName}}
        </option>
      </select>
    </div>
    <button type="submit" class="btn btn-primary">Save</button>
  </form>
</div>

When I click the dropdown option I get an object that represents the building that I selected. 当我单击下拉选项时,我得到一个代表所选建筑物的对象。 The result is 结果是

Object {id: 2, buildingName: "Building 2", active: true}
   active: true
   buildingName: "Building 2"
   id: 2

When I click the save button and look at the data that is associated with the formcontrol the building is no longer an object it is just the buildingId. 当我单击保存按钮并查看与formcontrol关联的数据时,建筑物不再是对象,而只是buildingId。

Object {id: 1, unit: "Item 1", active: true, building: "2"}
   active: true
   building: "2"
   id: 1
   unit:"Item 1"
   __proto__: Object

How do I get the main equipment object to be set with the building object and not just the building id? 如何获得要与建筑物对象一起设置的主要设备对象,而不仅仅是建筑物ID?

It is because you had value attribute with building.id , that's why it is assigning building.id on option selection. 这是因为您具有building.id value属性,这就是为什么它在选择选项时分配building.id的原因。 You should consider changing your value attribute binding to ngValue with whole building object. 您应该考虑将整个building对象的value属性绑定更改为ngValue

<select class="form-control" formControlName="building" (change)="onSelect($event.target.value)">
  <option *ngFor="let building of buildingList" [ngValue]="building">
    {{building.buildingName}}
  </option>
</select>

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

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