简体   繁体   English

如何从 valueChanges 获取选定的对象,但仅使用 Angular 反应形式将对象 id 绑定到表单?

[英]How to get selected object from valueChanges but only bind object id to form using Angular reactive forms?

I have a select field that allows me to choose a car and the car id gets bound to the form.我有一个允许我选择汽车的选择字段,并且汽车 ID 绑定到表单。

<mat-form-field>
  <mat-label>Car</mat-label>
  <mat-select formControlName="carId">
    <mat-option *ngFor="let car of cars | async" [value]="car.carId">
      {{car.carName}}
    </mat-option>
  </mat-select>
</mat-form-field>

I'd like to get the instance of the car so that I can grab other info off of the object, but I can't do that using valueChanges as it only gives me the id:我想获取汽车的实例,以便我可以从对象中获取其他信息,但我不能使用 valueChanges 来做到这一点,因为它只给了我 id:

this.form.get('carId').valueChanges.subscribe(carId => { ... );

I could change the select field to bind the object instead of the id like this:我可以更改选择字段以绑定对象而不是像这样的 id:

<mat-form-field>
  <mat-label>Car</mat-label>
  <mat-select formControlName="carId">
    <mat-option *ngFor="let car of cars | async" [value]="car">
      {{car.carName}}
    </mat-option>
  </mat-select>
</mat-form-field>

But then the whole object is being bound to the form instead of just the id which would mess up my form submission.但随后整个对象都被绑定到表单,而不仅仅是 id,这会弄乱我的表单提交。

Is there an elegant way to get the selected object but still bind just the id to the form?有没有一种优雅的方法来获取选定的对象,但仍然只将 id 绑定到表单?

You have the carId so just lookup the car object in your cars array in the valueChanges.您有 carId,因此只需在 valueChanges 中的汽车数组中查找汽车对象。

It is much easier to change to the car value instead of the id and change your submit logic though.更改为 car 值而不是 id 并更改提交逻辑要容易得多。

It's a little clunky, but I found a way that I can live with.这有点笨拙,但我找到了一种可以接受的方式。 I bind my select field to a stand-alone FormControl so changes to it don't affect the form by using [formControl]= instead of formControlName=我将我的选择字段绑定到一个独立的 FormControl,因此通过使用[formControl]=而不是formControlName=对其进行的更改不会影响表单

<mat-form-field>
  <mat-label>Car</mat-label>
  <mat-select [formControl]="car">
    <mat-option *ngFor="let car of cars | async" [value]="car">
      {{car.carName}}
    </mat-option>
  </mat-select>
</mat-form-field>

Then I can subscribe to changes on that, do what I need with the car, and set the carId on the form.然后我可以订阅更改,对汽车执行我需要的操作,并在表单上设置 carId。

this.car = new FormControl([null, Validators.required]);
this.car.valueChanges.subscribe(selectedCar => {
  // Do whatever with selectedCar here
  this.form.get('carId').setValue(selectedCar ? selectedCar.carId : null);
});

This works, but to get it to work with Angular Material error handling (so the field turns red if not specified) I had to add a hidden input bound to the carId .这有效,但要使其与 Angular Material 错误处理一起工作(因此如果未指定该字段会变为红色),我必须添加一个隐藏的输入绑定到carId

<mat-form-field>
  <mat-label>Car</mat-label>
  <input matInput formControlName="carId" style="display:none">
  <mat-select [formControl]="car">
    <mat-option></mat-option>
    <mat-option *ngFor="let car of cars | async" [value]="car">
      {{car.carName}}
    </mat-option>
  </mat-select>
  <mat-error *ngIf="form.get('carId').hasError('required')">
    Car is required
  </mat-error>
</mat-form-field>

Update I'm still not happy with this solution because I'd also have to ensure that the car select is in sync when form.setValue() is called, which means I'd have to lookup the car from it's id - so I might as well do the lookup on the select change or modify the submit logic like Alexander's answer says.更新我仍然对这个解决方案不满意,因为我还必须确保在调用form.setValue()car选择是同步的,这意味着我必须从它的 id 中查找汽车 - 所以我也可以像亚历山大的回答所说的那样对选择更改进行查找或修改提交逻辑。

I'll keep this answer out here in case it helps anyone, but I'm still open to other ideas.我会保留这个答案,以防它对任何人有帮助,但我仍然对其他想法持开放态度。

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

相关问题 如何仅在输入模糊时触发反应形式 valueChanges 订阅并在 Angular 2 中输入键 - How to trigger reactive form valueChanges subscription only on input blur and enter key in Angular 2 Angular 5反应形式valueChanges不起作用 - Angular 5 reactive form valueChanges does not work 如何使用 Angular 中的反应式表单更新错误消息对象? - How to update error messages object with Reactive forms in Angular? 如何以角度(反应形式)从父组件形式迭代数组 - How to iterate an array from parent componenent form in angular (reactive forms) 返回 Angular 反应形式的选定属性内的所有 object 值 - Return all object values within selected selected property for Angular reactive form 如何使用反应式表单获取完整的下拉对象项? - How to get full drop-down object item using Reactive Forms? 如何以角度反应形式设置对象键值 - How to set the object key value in an angular reactive form 如何使用角度反应形式来生成复杂的JSON对象 - How to use angular reactive form to generate a complex JSON object 如何使用以前保存的 object 在 angular 反应形式中设置默认值? - How to set default values in angular reactive form using previously saved object? 如何在Angular中使用ngFor获取嵌套对象数组的选定值? - How to get selected value of nested object array using ngFor in Angular?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM