How to bind user details in form input values in Angular?












0














I am trying to bind user data in input values. The point is that in the form were already used ng-for for getting data for selects. However, I can't figure out how to bind data on input values. I thought there should be other ng for in form but it's not working. This page is the user settings component where the user will see his/her details in inputs and can update them.



TypeScript



import { Component, OnInit} from '@angular/core';
import { FormArray, FormControl, FormGroup, Validators,ControlValueAccessor,ValidatorFn,ValidationErrors} from '@angular/forms';
import { Router } from '@angular/router';
import {country, Countries} from "../../models/countries.model";



@Component({
selector: 'app-settings',
templateUrl: './settings.component.html',
styleUrls: ['./settings.component.css']
})
export class SettingsComponent implements OnInit {
users: any = [
{
username: 'Jess',
name: 'Jessica',
lastname: 'Scott',
gender: 'Female',
},
];
SettingsForm: FormGroup;
forbiddenEmails: any;
errorMessage: string;

countries:Countries=country;

genders = [
"Male",
"Female",
"Other"
];



constructor (
public nav: HeaderService,
private authService: AuthService,
private router: Router,
private tokenService: TokenService) {
this.SettingsForm = new FormGroup({
'username': new FormControl(null, [Validators.required,
Validators.pattern('(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])'),
]),
'name': new FormControl(null, [Validators.required,
Validators.pattern('(?=.*[a-z])(?=.*[A-Z])'),
]),
...
...
'email': new FormControl(null, [Validators.required, Validators.email], this.forbiddenEmails),
'country': new FormControl(this.countries[0], [Validators.required, ]),
'gender': new FormControl(this.genders[0], [Validators.required, ]),

});
}


ngOnInit() {
}

onSubmit() {
this.SettingsForm.reset();
}

}


HTML



<form action="" [formGroup]="SettingsForm" *ngFor="let user of users" (ngSubmit)="settingsUser()">
<div class="form-group form-settings">
<input _ngcontent-c0="" class="form-control form-control-lg" placeholder="username" type="text" id="username" formControlName="username"/>
<span *ngIf="!SettingsForm.get('username').valid && SettingsForm.get('username').touched" class="help-block">Please enter a valid username!</span>
</div>
<div class="form-group form-settings">
<input _ngcontent-c0="" class="form-control form-control-lg" placeholder="email" type="text" id="email" formControlName="email"/>
<span *ngIf="!SettingsForm.get('email').valid && SettingsForm.get('email').touched" class="help-block">Please enter a valid email!</span>
</div>
<div class="row">
<div class="form-group form-settings col-md-12">
<select class="form-control form-control-lg">
<option value="">--Please choose an option--</option>
<option ngDefaultControl [ngValue]="country" *ngFor="let country of countries" formControlName="country">{{country.name}}</option>
</select>
<span *ngIf="!SettingsForm.get('country').valid && SettingsForm.get('country').touched" class="help-block">Please enter a country!</span>
</div>
</div>
<div class="row">
<div class="form-group form-settings col-md-12">
<select class="form-control form-control-lg">
<option value="">--Gender--</option>
<option ngDefaultControl [ngValue]="gender" *ngFor="let gender of genders" formControlName="gender">{{gender}}</option>
</select>
<span *ngIf="!SettingsForm.get('gender').valid && SettingsForm.get('gender').touched" class="help-block">Please choos a gender!</span>
</div>
<div class="form-group form-settings">
<div>

<button type="submit" class=" btn form-btn btn-lg btn-block submit-btn">Sign In With Email</button>

</div>
</div>
</form>


How can display user details from the array on each input or select as a value?










share|improve this question





























    0














    I am trying to bind user data in input values. The point is that in the form were already used ng-for for getting data for selects. However, I can't figure out how to bind data on input values. I thought there should be other ng for in form but it's not working. This page is the user settings component where the user will see his/her details in inputs and can update them.



    TypeScript



    import { Component, OnInit} from '@angular/core';
    import { FormArray, FormControl, FormGroup, Validators,ControlValueAccessor,ValidatorFn,ValidationErrors} from '@angular/forms';
    import { Router } from '@angular/router';
    import {country, Countries} from "../../models/countries.model";



    @Component({
    selector: 'app-settings',
    templateUrl: './settings.component.html',
    styleUrls: ['./settings.component.css']
    })
    export class SettingsComponent implements OnInit {
    users: any = [
    {
    username: 'Jess',
    name: 'Jessica',
    lastname: 'Scott',
    gender: 'Female',
    },
    ];
    SettingsForm: FormGroup;
    forbiddenEmails: any;
    errorMessage: string;

    countries:Countries=country;

    genders = [
    "Male",
    "Female",
    "Other"
    ];



    constructor (
    public nav: HeaderService,
    private authService: AuthService,
    private router: Router,
    private tokenService: TokenService) {
    this.SettingsForm = new FormGroup({
    'username': new FormControl(null, [Validators.required,
    Validators.pattern('(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])'),
    ]),
    'name': new FormControl(null, [Validators.required,
    Validators.pattern('(?=.*[a-z])(?=.*[A-Z])'),
    ]),
    ...
    ...
    'email': new FormControl(null, [Validators.required, Validators.email], this.forbiddenEmails),
    'country': new FormControl(this.countries[0], [Validators.required, ]),
    'gender': new FormControl(this.genders[0], [Validators.required, ]),

    });
    }


    ngOnInit() {
    }

    onSubmit() {
    this.SettingsForm.reset();
    }

    }


    HTML



    <form action="" [formGroup]="SettingsForm" *ngFor="let user of users" (ngSubmit)="settingsUser()">
    <div class="form-group form-settings">
    <input _ngcontent-c0="" class="form-control form-control-lg" placeholder="username" type="text" id="username" formControlName="username"/>
    <span *ngIf="!SettingsForm.get('username').valid && SettingsForm.get('username').touched" class="help-block">Please enter a valid username!</span>
    </div>
    <div class="form-group form-settings">
    <input _ngcontent-c0="" class="form-control form-control-lg" placeholder="email" type="text" id="email" formControlName="email"/>
    <span *ngIf="!SettingsForm.get('email').valid && SettingsForm.get('email').touched" class="help-block">Please enter a valid email!</span>
    </div>
    <div class="row">
    <div class="form-group form-settings col-md-12">
    <select class="form-control form-control-lg">
    <option value="">--Please choose an option--</option>
    <option ngDefaultControl [ngValue]="country" *ngFor="let country of countries" formControlName="country">{{country.name}}</option>
    </select>
    <span *ngIf="!SettingsForm.get('country').valid && SettingsForm.get('country').touched" class="help-block">Please enter a country!</span>
    </div>
    </div>
    <div class="row">
    <div class="form-group form-settings col-md-12">
    <select class="form-control form-control-lg">
    <option value="">--Gender--</option>
    <option ngDefaultControl [ngValue]="gender" *ngFor="let gender of genders" formControlName="gender">{{gender}}</option>
    </select>
    <span *ngIf="!SettingsForm.get('gender').valid && SettingsForm.get('gender').touched" class="help-block">Please choos a gender!</span>
    </div>
    <div class="form-group form-settings">
    <div>

    <button type="submit" class=" btn form-btn btn-lg btn-block submit-btn">Sign In With Email</button>

    </div>
    </div>
    </form>


    How can display user details from the array on each input or select as a value?










    share|improve this question



























      0












      0








      0







      I am trying to bind user data in input values. The point is that in the form were already used ng-for for getting data for selects. However, I can't figure out how to bind data on input values. I thought there should be other ng for in form but it's not working. This page is the user settings component where the user will see his/her details in inputs and can update them.



      TypeScript



      import { Component, OnInit} from '@angular/core';
      import { FormArray, FormControl, FormGroup, Validators,ControlValueAccessor,ValidatorFn,ValidationErrors} from '@angular/forms';
      import { Router } from '@angular/router';
      import {country, Countries} from "../../models/countries.model";



      @Component({
      selector: 'app-settings',
      templateUrl: './settings.component.html',
      styleUrls: ['./settings.component.css']
      })
      export class SettingsComponent implements OnInit {
      users: any = [
      {
      username: 'Jess',
      name: 'Jessica',
      lastname: 'Scott',
      gender: 'Female',
      },
      ];
      SettingsForm: FormGroup;
      forbiddenEmails: any;
      errorMessage: string;

      countries:Countries=country;

      genders = [
      "Male",
      "Female",
      "Other"
      ];



      constructor (
      public nav: HeaderService,
      private authService: AuthService,
      private router: Router,
      private tokenService: TokenService) {
      this.SettingsForm = new FormGroup({
      'username': new FormControl(null, [Validators.required,
      Validators.pattern('(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])'),
      ]),
      'name': new FormControl(null, [Validators.required,
      Validators.pattern('(?=.*[a-z])(?=.*[A-Z])'),
      ]),
      ...
      ...
      'email': new FormControl(null, [Validators.required, Validators.email], this.forbiddenEmails),
      'country': new FormControl(this.countries[0], [Validators.required, ]),
      'gender': new FormControl(this.genders[0], [Validators.required, ]),

      });
      }


      ngOnInit() {
      }

      onSubmit() {
      this.SettingsForm.reset();
      }

      }


      HTML



      <form action="" [formGroup]="SettingsForm" *ngFor="let user of users" (ngSubmit)="settingsUser()">
      <div class="form-group form-settings">
      <input _ngcontent-c0="" class="form-control form-control-lg" placeholder="username" type="text" id="username" formControlName="username"/>
      <span *ngIf="!SettingsForm.get('username').valid && SettingsForm.get('username').touched" class="help-block">Please enter a valid username!</span>
      </div>
      <div class="form-group form-settings">
      <input _ngcontent-c0="" class="form-control form-control-lg" placeholder="email" type="text" id="email" formControlName="email"/>
      <span *ngIf="!SettingsForm.get('email').valid && SettingsForm.get('email').touched" class="help-block">Please enter a valid email!</span>
      </div>
      <div class="row">
      <div class="form-group form-settings col-md-12">
      <select class="form-control form-control-lg">
      <option value="">--Please choose an option--</option>
      <option ngDefaultControl [ngValue]="country" *ngFor="let country of countries" formControlName="country">{{country.name}}</option>
      </select>
      <span *ngIf="!SettingsForm.get('country').valid && SettingsForm.get('country').touched" class="help-block">Please enter a country!</span>
      </div>
      </div>
      <div class="row">
      <div class="form-group form-settings col-md-12">
      <select class="form-control form-control-lg">
      <option value="">--Gender--</option>
      <option ngDefaultControl [ngValue]="gender" *ngFor="let gender of genders" formControlName="gender">{{gender}}</option>
      </select>
      <span *ngIf="!SettingsForm.get('gender').valid && SettingsForm.get('gender').touched" class="help-block">Please choos a gender!</span>
      </div>
      <div class="form-group form-settings">
      <div>

      <button type="submit" class=" btn form-btn btn-lg btn-block submit-btn">Sign In With Email</button>

      </div>
      </div>
      </form>


      How can display user details from the array on each input or select as a value?










      share|improve this question















      I am trying to bind user data in input values. The point is that in the form were already used ng-for for getting data for selects. However, I can't figure out how to bind data on input values. I thought there should be other ng for in form but it's not working. This page is the user settings component where the user will see his/her details in inputs and can update them.



      TypeScript



      import { Component, OnInit} from '@angular/core';
      import { FormArray, FormControl, FormGroup, Validators,ControlValueAccessor,ValidatorFn,ValidationErrors} from '@angular/forms';
      import { Router } from '@angular/router';
      import {country, Countries} from "../../models/countries.model";



      @Component({
      selector: 'app-settings',
      templateUrl: './settings.component.html',
      styleUrls: ['./settings.component.css']
      })
      export class SettingsComponent implements OnInit {
      users: any = [
      {
      username: 'Jess',
      name: 'Jessica',
      lastname: 'Scott',
      gender: 'Female',
      },
      ];
      SettingsForm: FormGroup;
      forbiddenEmails: any;
      errorMessage: string;

      countries:Countries=country;

      genders = [
      "Male",
      "Female",
      "Other"
      ];



      constructor (
      public nav: HeaderService,
      private authService: AuthService,
      private router: Router,
      private tokenService: TokenService) {
      this.SettingsForm = new FormGroup({
      'username': new FormControl(null, [Validators.required,
      Validators.pattern('(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])'),
      ]),
      'name': new FormControl(null, [Validators.required,
      Validators.pattern('(?=.*[a-z])(?=.*[A-Z])'),
      ]),
      ...
      ...
      'email': new FormControl(null, [Validators.required, Validators.email], this.forbiddenEmails),
      'country': new FormControl(this.countries[0], [Validators.required, ]),
      'gender': new FormControl(this.genders[0], [Validators.required, ]),

      });
      }


      ngOnInit() {
      }

      onSubmit() {
      this.SettingsForm.reset();
      }

      }


      HTML



      <form action="" [formGroup]="SettingsForm" *ngFor="let user of users" (ngSubmit)="settingsUser()">
      <div class="form-group form-settings">
      <input _ngcontent-c0="" class="form-control form-control-lg" placeholder="username" type="text" id="username" formControlName="username"/>
      <span *ngIf="!SettingsForm.get('username').valid && SettingsForm.get('username').touched" class="help-block">Please enter a valid username!</span>
      </div>
      <div class="form-group form-settings">
      <input _ngcontent-c0="" class="form-control form-control-lg" placeholder="email" type="text" id="email" formControlName="email"/>
      <span *ngIf="!SettingsForm.get('email').valid && SettingsForm.get('email').touched" class="help-block">Please enter a valid email!</span>
      </div>
      <div class="row">
      <div class="form-group form-settings col-md-12">
      <select class="form-control form-control-lg">
      <option value="">--Please choose an option--</option>
      <option ngDefaultControl [ngValue]="country" *ngFor="let country of countries" formControlName="country">{{country.name}}</option>
      </select>
      <span *ngIf="!SettingsForm.get('country').valid && SettingsForm.get('country').touched" class="help-block">Please enter a country!</span>
      </div>
      </div>
      <div class="row">
      <div class="form-group form-settings col-md-12">
      <select class="form-control form-control-lg">
      <option value="">--Gender--</option>
      <option ngDefaultControl [ngValue]="gender" *ngFor="let gender of genders" formControlName="gender">{{gender}}</option>
      </select>
      <span *ngIf="!SettingsForm.get('gender').valid && SettingsForm.get('gender').touched" class="help-block">Please choos a gender!</span>
      </div>
      <div class="form-group form-settings">
      <div>

      <button type="submit" class=" btn form-btn btn-lg btn-block submit-btn">Sign In With Email</button>

      </div>
      </div>
      </form>


      How can display user details from the array on each input or select as a value?







      angular typescript angular6 angular-forms






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 at 16:43

























      asked Nov 21 at 9:49









      NewTech Lover

      102210




      102210





























          active

          oldest

          votes











          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%2f53409287%2fhow-to-bind-user-details-in-form-input-values-in-angular%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53409287%2fhow-to-bind-user-details-in-form-input-values-in-angular%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

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python