简体   繁体   English

Angular 反应性 Forms map 仅是 ZA8CFDE6331BD59EB2AC96F8911C4B6666 的一部分

[英]Angular Reactive Forms map only part of object to formControlName

I have a select input that chooses from a list of objects, displays a name, and also binds it into a form's field.我有一个 select 输入,它从对象列表中进行选择,显示名称,并将其绑定到表单的字段中。 However, I need that whole object available for the change event.但是,我需要整个 object 可用于change事件。 Changing formControlName is not really an option.更改formControlName并不是一个真正的选择。

HTML: HTML:

<form [formGroup]="form">
  <!-- Here I can access $event.target.value to obtain just the userName -->
  <select formControlName="userName" (change)="process($event.target.value)">
    <option [value]="user.name" *ngFor="let user of users">{{ user.name }}</option>
  </select>

  <!-- I would like to have access to the entire object -->
  <!-- <select formControlName="userName" (change)="processUser(user)" -->
  <!--   <option *ngFor="let user of users">{{ user.name }}</option> -->
  <!-- </select> -->
</form>

<span>The value is: {{ form.get('userName').value }}</span>

TS: TS:

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
  form: FormGroup;

  users = [
    { id: 1, name: 'John' },
    { id: 2, name: 'Mary' },
    { id: 3, name: 'Michael' }
  ]

  constructor(private fb: FormBuilder) {}

  ngOnInit() {
    this.form = this.fb.group({ userName: [null] });
  }

  process(userName) {
    console.log(userName);
  }

  // Here I need the entire user object
  processUser(user) {
    // do stuff
  }

  // I can always do this (in my case names are guaranteed to be unique)
  // but it's a really ugly workaround
  processUserWorkaround(userName) {
    const user = this.users.find(u => u.name == userName);
    //do stuff
  }
}

Full Stackblitz 全栈闪电战

You can access the selected index on the change event with $event.target.selectedIndex .您可以使用$event.target.selectedIndex访问更改事件上的选定索引。 You can use this index to get the whole object from the users array and passed that object to processUser method.您可以使用此索引从users数组中获取整个 object 并将该 object 传递给processUser方法。

Example例子

<select
  formControlName="userName"
  (change)="processUser(users[$event.target.selectedIndex])"
>
  <option *ngFor="let user of users" [value]="user.name">{{ user.name }}</option>
</select>

Stackblitz堆栈闪电战

https://stackblitz.com/edit/angular-7kjrps https://stackblitz.com/edit/angular-7kjrps

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

相关问题 Angular 反应式 forms:将值绑定到 formcontrolname - Angular reactive forms: bind value to formcontrolname Angular 2 反应式表单 - 从 formControlName 管道传递一个值 - Angular 2 reactive forms - piping a value from a formControlName 如何以反应形式绑定,即 angular 中的 formcontrolname 和 ngmodel - How to bind in reactive forms which is the formcontrolname and ngmodel in angular 如何从子组件将“formControlName”添加到角度反应形式“formGroup”? - How to add "formControlName" to angular reactive forms "formGroup" from child components? 在angular的反应式forms中,为什么formControlName中传递的参数是作为字符串传递的? - In reactive forms of angular, why the parameter passed in formControlName is passed as a string? Angular 反应 forms:使用单个 formControlName 同步多个输入 - Angular reactive forms: sync multiple inputs with a single formControlName 如何获取值在 Angular 中更改的字段的 FormControlName 反应 forms - How to get FormControlName of the field which value changed in Angular reactive forms 仅在输入中显示对象名称 | FormControlName - 角度 - display only object name in input | FormControlName - Angular 使用Angular反应形式编辑对象 - Editing object with Angular reactive forms RadioButtonGroup-formControlName不适用于反应式表单 - RadioButtonGroup-formControlName not working with reactive forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM