Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

199
Views
How to create prefilled form in Angular2?

Hi I want to have prefilled form in Angular2 and not allow users to change the prefilled data. I just want to post this prefilled data to server (the data is automatically derived from currently logged in user's information).

How can I achieve this?

my code.

<form [formGroup]="requestForm" (ngSubmit)="onSubmit()">
    <div class="form-group">

<label class="fontstyle1"><span class="fontstyle2">STEP2. </span>User Information</label>
  <div class="form-group">
      <label class="fontstyle3">Name</label>
      <input *ngFor="let user of currentUser | getValues | slice:1:2" type="text" required class="form-control" formControlName="name"[(ngModel)]="selected_name" value="user.name">
    </div>

  <div class="form-group">
      <label class="fontstyle3">E-mail</label>
      <input *ngFor="let user of currentUser | getValues | slice:1:2" type="text" required class="form-control" formControlName="email" [(ngModel)]="selected_email" value="user.email">
    </div>

      <div class="form-group">
      <label class="fontstyle3">Phone</label>
      <input *ngFor="let user of currentUser | getValues | slice:1:2" type="text" required class="form-control" formControlName="phone" [(ngModel)]="selected_phone" value="user.phone">
    </div>

    <br>

        <button type="submit" id="request-btn">Request</button>

</div>

When I run this, the result is neither unchangeable nor prefilled.

Help!

Thank you !

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Try to use either formControlName or ngModel, And also disable the input fields.

<form [formGroup]="requestForm" (ngSubmit)="onSubmit()">
    <div class="form-group">    
      <label class="fontstyle1"><span class="fontstyle2">STEP2. </span>User Information</label>
         <div class="form-group">
            <label class="fontstyle3">Name</label>
            <input *ngFor="let user of currentUser | getValues | slice:1:2" type="text" required class="form-control" [(ngModel)]="user.name" disabled>
         </div>

         <div class="form-group">
            <label class="fontstyle3">E-mail</label>
            <input *ngFor="let user of currentUser | getValues | slice:1:2" type="text" required class="form-control" [(ngModel)]="user.email" disabled>
         </div>

         <div class="form-group">
            <label class="fontstyle3">Phone</label>
            <input *ngFor="let user of currentUser | getValues | slice:1:2" type="text" required class="form-control" [(ngModel)]="user.phone" disabled>
         </div>    
         <br>    
         <button type="submit" id="request-btn">Request</button>   
      </div>
   </div>
</form>

And in your component on ngOnInit set the data to form

ngOnInit(){
   this.apicall.subscribe((info)=>{
       this.currentUser = info;
    }
  );
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!