Angular form submit only has fields with changes - not the full form values












3















When i submit my angular form i get values only for changed fields. I should be getting the full form values correct?



 // Profile.html
<form [formGroup]="profileForm" (ngSubmit)="submitProfileForm(profileForm.value)" #myform="ngForm">

<input type="text" class="form-control" id="firstName" value="{{currentUserMeta.firstName}}" formControlName='firstName'>

<input type="text" class="form-control" id="lastName" formControlName='lastName'value="{{currentUserMeta.lastName}}">
....
</form>

// Profile.ts
submitProfileForm(value: any) {
console.log(this.profileForm.get('firstName').value);
console.log(this.profileForm.get('lastName').value);
console.log(value);
this.userService.updateUserProfile(value);
}


When I change only the about input field for example and then press submit I get this in the output.



{displayName: "", firstName: "", lastName: "", about: "12"}


firstName and lastName had values and they were grabbed from an observable currentUserMeta



It seems to me the form is not aware when these values got updated through angular binding {{ }}.



another example



form initial state (Correct)



enter image description here



after pressing 2 for lastName and 3 for about field and then pressing Submit



enter image description here



As you can see firstName value of 1 is not there in the log. And even trying to print it out through log is not working.



 console.log(this.profileForm.get('firstName').value);


I should add that I get all the values(changed and unchanged) if the form is already loaded and I updated all of the inputs at one time or another. (I dont redirect to another page after submit). Let me know if I have done something wrong.










share|improve this question



























    3















    When i submit my angular form i get values only for changed fields. I should be getting the full form values correct?



     // Profile.html
    <form [formGroup]="profileForm" (ngSubmit)="submitProfileForm(profileForm.value)" #myform="ngForm">

    <input type="text" class="form-control" id="firstName" value="{{currentUserMeta.firstName}}" formControlName='firstName'>

    <input type="text" class="form-control" id="lastName" formControlName='lastName'value="{{currentUserMeta.lastName}}">
    ....
    </form>

    // Profile.ts
    submitProfileForm(value: any) {
    console.log(this.profileForm.get('firstName').value);
    console.log(this.profileForm.get('lastName').value);
    console.log(value);
    this.userService.updateUserProfile(value);
    }


    When I change only the about input field for example and then press submit I get this in the output.



    {displayName: "", firstName: "", lastName: "", about: "12"}


    firstName and lastName had values and they were grabbed from an observable currentUserMeta



    It seems to me the form is not aware when these values got updated through angular binding {{ }}.



    another example



    form initial state (Correct)



    enter image description here



    after pressing 2 for lastName and 3 for about field and then pressing Submit



    enter image description here



    As you can see firstName value of 1 is not there in the log. And even trying to print it out through log is not working.



     console.log(this.profileForm.get('firstName').value);


    I should add that I get all the values(changed and unchanged) if the form is already loaded and I updated all of the inputs at one time or another. (I dont redirect to another page after submit). Let me know if I have done something wrong.










    share|improve this question

























      3












      3








      3


      1






      When i submit my angular form i get values only for changed fields. I should be getting the full form values correct?



       // Profile.html
      <form [formGroup]="profileForm" (ngSubmit)="submitProfileForm(profileForm.value)" #myform="ngForm">

      <input type="text" class="form-control" id="firstName" value="{{currentUserMeta.firstName}}" formControlName='firstName'>

      <input type="text" class="form-control" id="lastName" formControlName='lastName'value="{{currentUserMeta.lastName}}">
      ....
      </form>

      // Profile.ts
      submitProfileForm(value: any) {
      console.log(this.profileForm.get('firstName').value);
      console.log(this.profileForm.get('lastName').value);
      console.log(value);
      this.userService.updateUserProfile(value);
      }


      When I change only the about input field for example and then press submit I get this in the output.



      {displayName: "", firstName: "", lastName: "", about: "12"}


      firstName and lastName had values and they were grabbed from an observable currentUserMeta



      It seems to me the form is not aware when these values got updated through angular binding {{ }}.



      another example



      form initial state (Correct)



      enter image description here



      after pressing 2 for lastName and 3 for about field and then pressing Submit



      enter image description here



      As you can see firstName value of 1 is not there in the log. And even trying to print it out through log is not working.



       console.log(this.profileForm.get('firstName').value);


      I should add that I get all the values(changed and unchanged) if the form is already loaded and I updated all of the inputs at one time or another. (I dont redirect to another page after submit). Let me know if I have done something wrong.










      share|improve this question














      When i submit my angular form i get values only for changed fields. I should be getting the full form values correct?



       // Profile.html
      <form [formGroup]="profileForm" (ngSubmit)="submitProfileForm(profileForm.value)" #myform="ngForm">

      <input type="text" class="form-control" id="firstName" value="{{currentUserMeta.firstName}}" formControlName='firstName'>

      <input type="text" class="form-control" id="lastName" formControlName='lastName'value="{{currentUserMeta.lastName}}">
      ....
      </form>

      // Profile.ts
      submitProfileForm(value: any) {
      console.log(this.profileForm.get('firstName').value);
      console.log(this.profileForm.get('lastName').value);
      console.log(value);
      this.userService.updateUserProfile(value);
      }


      When I change only the about input field for example and then press submit I get this in the output.



      {displayName: "", firstName: "", lastName: "", about: "12"}


      firstName and lastName had values and they were grabbed from an observable currentUserMeta



      It seems to me the form is not aware when these values got updated through angular binding {{ }}.



      another example



      form initial state (Correct)



      enter image description here



      after pressing 2 for lastName and 3 for about field and then pressing Submit



      enter image description here



      As you can see firstName value of 1 is not there in the log. And even trying to print it out through log is not working.



       console.log(this.profileForm.get('firstName').value);


      I should add that I get all the values(changed and unchanged) if the form is already loaded and I updated all of the inputs at one time or another. (I dont redirect to another page after submit). Let me know if I have done something wrong.







      angular observable angular-forms






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 0:13









      DavvitDavvit

      311113




      311113
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Your aproach is not 100% fine, it seams that you are using ModelDrivenForm and passing the value which is more like TemplateDrivenForm.



          If you use ModelDrivenForm, like it seams that is what you are using, is fine how you are declaring the form's [formGroup] and the input's formControlName but you shuldn't put the predefined values in value="{{currentUserMeta.lastName}}"



          You shuld set these values in your ts file, using for example this.profileForm.get('lastName').set(this.currentUserMeta.lastName); when you have this data.



          I hope that this helps, more info about ModelDrivenForms are here in the doc https://angular.io/guide/reactive-forms and about template-driven form are here https://angular.io/guide/forms






          share|improve this answer
























          • you are right! its described here. angular.io/api/forms/FormControlName#use-with-ngmodel since the data source is observable I am thinking to set the values in a subscribe in ngOnInit() as such. Do you see any problem with this? this.currentUserMeta.subscribe((data) => { this.profileForm.get('lastName').setValue(data.lastName); });

            – Davvit
            Nov 21 '18 at 4:22













          • I don't see any problem with that, and if in that subscribe you get in data the two needed values what you can do to set both of them at once is tu use patchValue() like this: this.currentUserMeta.subscribe((data) => { this.profileForm.patchValue( { firstName: data.firstName lastName: data.lastName }); });

            – Octavio Garbarino
            Nov 21 '18 at 13:01






          • 1





            nice thanks, Octavio!

            – Davvit
            Nov 21 '18 at 14:38











          • You are welcome Davvit!

            – Octavio Garbarino
            Nov 21 '18 at 14:39











          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%2f53403520%2fangular-form-submit-only-has-fields-with-changes-not-the-full-form-values%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Your aproach is not 100% fine, it seams that you are using ModelDrivenForm and passing the value which is more like TemplateDrivenForm.



          If you use ModelDrivenForm, like it seams that is what you are using, is fine how you are declaring the form's [formGroup] and the input's formControlName but you shuldn't put the predefined values in value="{{currentUserMeta.lastName}}"



          You shuld set these values in your ts file, using for example this.profileForm.get('lastName').set(this.currentUserMeta.lastName); when you have this data.



          I hope that this helps, more info about ModelDrivenForms are here in the doc https://angular.io/guide/reactive-forms and about template-driven form are here https://angular.io/guide/forms






          share|improve this answer
























          • you are right! its described here. angular.io/api/forms/FormControlName#use-with-ngmodel since the data source is observable I am thinking to set the values in a subscribe in ngOnInit() as such. Do you see any problem with this? this.currentUserMeta.subscribe((data) => { this.profileForm.get('lastName').setValue(data.lastName); });

            – Davvit
            Nov 21 '18 at 4:22













          • I don't see any problem with that, and if in that subscribe you get in data the two needed values what you can do to set both of them at once is tu use patchValue() like this: this.currentUserMeta.subscribe((data) => { this.profileForm.patchValue( { firstName: data.firstName lastName: data.lastName }); });

            – Octavio Garbarino
            Nov 21 '18 at 13:01






          • 1





            nice thanks, Octavio!

            – Davvit
            Nov 21 '18 at 14:38











          • You are welcome Davvit!

            – Octavio Garbarino
            Nov 21 '18 at 14:39
















          1














          Your aproach is not 100% fine, it seams that you are using ModelDrivenForm and passing the value which is more like TemplateDrivenForm.



          If you use ModelDrivenForm, like it seams that is what you are using, is fine how you are declaring the form's [formGroup] and the input's formControlName but you shuldn't put the predefined values in value="{{currentUserMeta.lastName}}"



          You shuld set these values in your ts file, using for example this.profileForm.get('lastName').set(this.currentUserMeta.lastName); when you have this data.



          I hope that this helps, more info about ModelDrivenForms are here in the doc https://angular.io/guide/reactive-forms and about template-driven form are here https://angular.io/guide/forms






          share|improve this answer
























          • you are right! its described here. angular.io/api/forms/FormControlName#use-with-ngmodel since the data source is observable I am thinking to set the values in a subscribe in ngOnInit() as such. Do you see any problem with this? this.currentUserMeta.subscribe((data) => { this.profileForm.get('lastName').setValue(data.lastName); });

            – Davvit
            Nov 21 '18 at 4:22













          • I don't see any problem with that, and if in that subscribe you get in data the two needed values what you can do to set both of them at once is tu use patchValue() like this: this.currentUserMeta.subscribe((data) => { this.profileForm.patchValue( { firstName: data.firstName lastName: data.lastName }); });

            – Octavio Garbarino
            Nov 21 '18 at 13:01






          • 1





            nice thanks, Octavio!

            – Davvit
            Nov 21 '18 at 14:38











          • You are welcome Davvit!

            – Octavio Garbarino
            Nov 21 '18 at 14:39














          1












          1








          1







          Your aproach is not 100% fine, it seams that you are using ModelDrivenForm and passing the value which is more like TemplateDrivenForm.



          If you use ModelDrivenForm, like it seams that is what you are using, is fine how you are declaring the form's [formGroup] and the input's formControlName but you shuldn't put the predefined values in value="{{currentUserMeta.lastName}}"



          You shuld set these values in your ts file, using for example this.profileForm.get('lastName').set(this.currentUserMeta.lastName); when you have this data.



          I hope that this helps, more info about ModelDrivenForms are here in the doc https://angular.io/guide/reactive-forms and about template-driven form are here https://angular.io/guide/forms






          share|improve this answer













          Your aproach is not 100% fine, it seams that you are using ModelDrivenForm and passing the value which is more like TemplateDrivenForm.



          If you use ModelDrivenForm, like it seams that is what you are using, is fine how you are declaring the form's [formGroup] and the input's formControlName but you shuldn't put the predefined values in value="{{currentUserMeta.lastName}}"



          You shuld set these values in your ts file, using for example this.profileForm.get('lastName').set(this.currentUserMeta.lastName); when you have this data.



          I hope that this helps, more info about ModelDrivenForms are here in the doc https://angular.io/guide/reactive-forms and about template-driven form are here https://angular.io/guide/forms







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 0:42









          Octavio GarbarinoOctavio Garbarino

          472511




          472511













          • you are right! its described here. angular.io/api/forms/FormControlName#use-with-ngmodel since the data source is observable I am thinking to set the values in a subscribe in ngOnInit() as such. Do you see any problem with this? this.currentUserMeta.subscribe((data) => { this.profileForm.get('lastName').setValue(data.lastName); });

            – Davvit
            Nov 21 '18 at 4:22













          • I don't see any problem with that, and if in that subscribe you get in data the two needed values what you can do to set both of them at once is tu use patchValue() like this: this.currentUserMeta.subscribe((data) => { this.profileForm.patchValue( { firstName: data.firstName lastName: data.lastName }); });

            – Octavio Garbarino
            Nov 21 '18 at 13:01






          • 1





            nice thanks, Octavio!

            – Davvit
            Nov 21 '18 at 14:38











          • You are welcome Davvit!

            – Octavio Garbarino
            Nov 21 '18 at 14:39



















          • you are right! its described here. angular.io/api/forms/FormControlName#use-with-ngmodel since the data source is observable I am thinking to set the values in a subscribe in ngOnInit() as such. Do you see any problem with this? this.currentUserMeta.subscribe((data) => { this.profileForm.get('lastName').setValue(data.lastName); });

            – Davvit
            Nov 21 '18 at 4:22













          • I don't see any problem with that, and if in that subscribe you get in data the two needed values what you can do to set both of them at once is tu use patchValue() like this: this.currentUserMeta.subscribe((data) => { this.profileForm.patchValue( { firstName: data.firstName lastName: data.lastName }); });

            – Octavio Garbarino
            Nov 21 '18 at 13:01






          • 1





            nice thanks, Octavio!

            – Davvit
            Nov 21 '18 at 14:38











          • You are welcome Davvit!

            – Octavio Garbarino
            Nov 21 '18 at 14:39

















          you are right! its described here. angular.io/api/forms/FormControlName#use-with-ngmodel since the data source is observable I am thinking to set the values in a subscribe in ngOnInit() as such. Do you see any problem with this? this.currentUserMeta.subscribe((data) => { this.profileForm.get('lastName').setValue(data.lastName); });

          – Davvit
          Nov 21 '18 at 4:22







          you are right! its described here. angular.io/api/forms/FormControlName#use-with-ngmodel since the data source is observable I am thinking to set the values in a subscribe in ngOnInit() as such. Do you see any problem with this? this.currentUserMeta.subscribe((data) => { this.profileForm.get('lastName').setValue(data.lastName); });

          – Davvit
          Nov 21 '18 at 4:22















          I don't see any problem with that, and if in that subscribe you get in data the two needed values what you can do to set both of them at once is tu use patchValue() like this: this.currentUserMeta.subscribe((data) => { this.profileForm.patchValue( { firstName: data.firstName lastName: data.lastName }); });

          – Octavio Garbarino
          Nov 21 '18 at 13:01





          I don't see any problem with that, and if in that subscribe you get in data the two needed values what you can do to set both of them at once is tu use patchValue() like this: this.currentUserMeta.subscribe((data) => { this.profileForm.patchValue( { firstName: data.firstName lastName: data.lastName }); });

          – Octavio Garbarino
          Nov 21 '18 at 13:01




          1




          1





          nice thanks, Octavio!

          – Davvit
          Nov 21 '18 at 14:38





          nice thanks, Octavio!

          – Davvit
          Nov 21 '18 at 14:38













          You are welcome Davvit!

          – Octavio Garbarino
          Nov 21 '18 at 14:39





          You are welcome Davvit!

          – Octavio Garbarino
          Nov 21 '18 at 14:39




















          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%2f53403520%2fangular-form-submit-only-has-fields-with-changes-not-the-full-form-values%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







          Popular posts from this blog

          How to change which sound is reproduced for terminal bell?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

          Can I use Tabulator js library in my java Spring + Thymeleaf project?