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










1















I have a select field that allows me to choose a car and the car id gets bound to the form.



<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:



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


I could change the select field to bind the object instead of the id like this:



<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.



Is there an elegant way to get the selected object but still bind just the id to the form?










share|improve this question




























    1















    I have a select field that allows me to choose a car and the car id gets bound to the form.



    <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:



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


    I could change the select field to bind the object instead of the id like this:



    <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.



    Is there an elegant way to get the selected object but still bind just the id to the form?










    share|improve this question


























      1












      1








      1








      I have a select field that allows me to choose a car and the car id gets bound to the form.



      <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:



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


      I could change the select field to bind the object instead of the id like this:



      <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.



      Is there an elegant way to get the selected object but still bind just the id to the form?










      share|improve this question
















      I have a select field that allows me to choose a car and the car id gets bound to the form.



      <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:



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


      I could change the select field to bind the object instead of the id like this:



      <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.



      Is there an elegant way to get the selected object but still bind just the id to the form?







      javascript angular angular-reactive-forms






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 18:45







      adam0101

















      asked Nov 14 '18 at 17:25









      adam0101adam0101

      12.6k1661124




      12.6k1661124






















          2 Answers
          2






          active

          oldest

          votes


















          1














          You have the carId so just lookup the car object in your cars array in the valueChanges.



          It is much easier to change to the car value instead of the id and change your submit logic though.






          share|improve this answer






























            0














            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=



            <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.



            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.



            <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.



            I'll keep this answer out here in case it helps anyone, but I'm still open to other ideas.






            share|improve this answer
























              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305690%2fhow-to-get-selected-object-from-valuechanges-but-only-bind-object-id-to-form-usi%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              You have the carId so just lookup the car object in your cars array in the valueChanges.



              It is much easier to change to the car value instead of the id and change your submit logic though.






              share|improve this answer



























                1














                You have the carId so just lookup the car object in your cars array in the valueChanges.



                It is much easier to change to the car value instead of the id and change your submit logic though.






                share|improve this answer

























                  1












                  1








                  1







                  You have the carId so just lookup the car object in your cars array in the valueChanges.



                  It is much easier to change to the car value instead of the id and change your submit logic though.






                  share|improve this answer













                  You have the carId so just lookup the car object in your cars array in the valueChanges.



                  It is much easier to change to the car value instead of the id and change your submit logic though.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 17:56









                  AlexanderAlexander

                  1,378920




                  1,378920























                      0














                      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=



                      <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.



                      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.



                      <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.



                      I'll keep this answer out here in case it helps anyone, but I'm still open to other ideas.






                      share|improve this answer





























                        0














                        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=



                        <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.



                        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.



                        <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.



                        I'll keep this answer out here in case it helps anyone, but I'm still open to other ideas.






                        share|improve this answer



























                          0












                          0








                          0







                          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=



                          <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.



                          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.



                          <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.



                          I'll keep this answer out here in case it helps anyone, but I'm still open to other ideas.






                          share|improve this answer















                          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=



                          <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.



                          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.



                          <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.



                          I'll keep this answer out here in case it helps anyone, but I'm still open to other ideas.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 14 '18 at 19:34

























                          answered Nov 14 '18 at 18:56









                          adam0101adam0101

                          12.6k1661124




                          12.6k1661124



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305690%2fhow-to-get-selected-object-from-valuechanges-but-only-bind-object-id-to-form-usi%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              這個網誌中的熱門文章

                              Barbados

                              How to read a connectionString WITH PROVIDER in .NET Core?

                              Node.js Script on GitHub Pages or Amazon S3