1

I have an edit profile page. So when the user clicks on the edit profile page the users details should be displayed in the input tag fields in the HTML. But I am unable to display the details I am retrieving.

edit-profile.component.html

 <form novalidate (ngSubmit)="onFormSubmit(signupForm)" #signupForm="ngForm">
      <div class="form-inputs clearfix">
        <div class="row">
            <div class="col-md-6">
                <p>
                    <label class="required">First Name<span>*</span></label>
                    <input 
                      type="text" 

                      [value]="accountDetails.firstName"
                      name="firstName" 
                      [(ngModel)] = "user.firstName" 
                      #firstName = "ngModel"
                     required><p>{{accountDetails.firstName}} </p>
                      <span *ngIf="firstName.invalid && (firstName.dirty || firstName.touched)" class="input-error">
                          <span *ngIf = "firstName.errors?.required">
                              First Name field can't be blank
                          </span>
                        </span>
                  </p></form>

edit-profile.component.ts

    import { Component, OnInit } from '@angular/core';
import { ApiServiceProvider } from '../services/api.service';


@Component({
  selector: 'app-edit-profile',
  templateUrl: './edit-profile.component.html',
  styleUrls: ['./edit-profile.component.css']
})
export class EditProfileComponent implements OnInit {
  public user: any = {};
  public accountDetails: any = {}

  constructor(
    private api: ApiServiceProvider
  ) { }

  ngOnInit() {

    let profile = JSON.parse(localStorage.getItem("profile"));
    this.accountDetails = profile.user;
    console.log(this.accountDetails);
  }

  public onFormSubmit({ value, valid }: { value: any, valid: boolean }) {
    this.user = value;
    this.api.put("/users/editprofile", value.userId, false)
      .subscribe((data) => {

        console.log(data)
        localStorage.setItem("profile", JSON.stringify(data));

        location.href = "/profile"

      }, (err) => {
        alert("Registartion failed " + err);
      })

  }

}
0

1 Answer 1

1

you only populate your accountDetails variable but you try to bind your user variable.

so you have:

this.accountDetails = profile.user;

but you bind your input to the user variable:

 <input type="text" [(ngModel)]="user.firstName" #firstName="ngModel" required>

Either populate your user variable:

this.user = profile.user;

or bind to your account details variable:

<input type="text" [(ngModel)]="accountDetails.firstName" #firstName="ngModel" required>

Also you need to close your div clearfix, div row and div column correctly before closing the form tag. </div></div></div></form>

Sign up to request clarification or add additional context in comments.

2 Comments

can I use value={{user.firstName}} inside the input tag to display the first name of the retrieved used
[(ngModel)]="user.firstName" will set the value of the input so you do not need to write value=.... read here: angular.io/api/forms/NgModel#how-to-use

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.