0
votes

I've got a form where I'm asking a user to select a project title. Along with that project title (when selected), I'd like to also grab two more values associated with that selection - then, use those values to add to form data that I'll end up saving to my database. The select list option that's being looped over contains a lot of data, more than just the project title. I'd like to use some of that data when this selection is made.

In the HTML, I'm looping over 'projects' and asking a user to select one. Easy enough. When they select this choice they automatically populate the 'formControlName' with the value property. In essence, I'd like to also populate more 'formControlName's with this select value - kind of like using multiple VALUE propertys in this select. I do know that I can interpolate more in the value property but it overloads the 'formControlName'

There are two other properties of this 'project' selection I'd like to grab - projectManager._id and projectManager.email - I want to save those values along with the form when saving


----- HTML -----

<!-- PROJECT -->
        <div class="form-group col-sm-4">
          <label>Project</label>
          <select class="custom-select form-control" formControlName="project">
            <option *ngFor="let project of projects" value="{{project?._id}}">
              {{ project.projectTitle | titlecase }}
            </option>
          </select>
        </div>

----- TS ------

// INITIATE THE FORM
    this.requestForm = this.fb.group({
      userId: this.currentUser.id,
      userEmail: this.currentUser.email,
      projectManagerId: '',
      projectManagerEmail: '',
      department: '',
      project: '',
      episode: '',
      requestItems: this.fb.array([
        this.fb.group({
          timecodeIn: '',
          timecodeOut: '',
          reason: '',
          line: '',
          note: ''
        })
      ])
    });

1
I am sorry, what's your questions? - MoxxiManagarm
I'd like to access the other two properties (project.projectManager._id and project.projectManager.email) when I select the project in the select list. I'm having a hard time figuring out how to grab those other two values when a user makes a selection? Ultimately I'd like to add those values to other formControlName properties. Thanks! - mike varela

1 Answers

1
votes

What I usually do in these situations is fetch those properties in the valueChanges observable whenever a selection is made, since you don't want to mess with the project form control value. That would be:

this.requestForm.get('project').valueChanges
  .subscribe((projectId: number) => {
    const project = this.projects.find((project) => project.id === projectId);
    * do whatever else with the values *
  });

That way the changes are tied to the select form value changes, which is essentially what you want in a reactive form.