0

What I want to achieve: I want pre-populated data in my form and I also want to use formControlName as I want the data too.

My html file -

<div class="container">
    <p class="h4 my-5">Add New Result</p>
    <div class="row">
        <div class="col">
            <form (ngSubmit)="editStudent()" [formGroup]="studentEditForm" name="myForm">
                <div class="mb-3">
                  <label for="roll-no" class="form-label">Roll No.</label>
                  <input type="number" class="form-control" id="roll-no" name="rollno" formControlName="rollno" value="{{student.rollNo}}" placeholder="{{student.rollNo}}" autocomplete="off" required>
                </div>
                <div class="mb-3">
                  <label for="name" class="form-label">Name</label>
                  <input type="text" class="form-control" id="name" name="name" formControlName="name" value={{student.name}} required>
                </div>
                <div class="mb-3">
                    <label for="email" class="form-label">Email address</label>
                    <input type="email" class="form-control" id="email" name="email" formControlName="email" value={{student.email}} required>
                </div>
                <div class="mb-3">
                    <label for="score" class="form-label">Score</label>
                    <input type="number" class="form-control" id="score" name="score" formControlName="score" value={{student.score}} required>
                </div>
                <button type="submit" class="btn btn-primary" ng-click="submitForm(myForm.$valid)">Submit</button><button type="submit" class="btn btn-dark mx-4">Clear</button>
            </form>
        </div>
        <div class="col"></div>
        <div class="col"></div>
    </div>
</div>

In Input Tag, I want to use value attribute so I can get a default value but I get Empty fields and I think it is because formControlName is controlling the data in my form. Is there a way to get pre-populated data using value attribute along with formControlName attribute?

Placeholder attribute works fine but I want a default value.

1
  • 1
    You need to set your default value as the initial value of the formControl Commented Oct 3, 2022 at 6:01

4 Answers 4

2

if you want a default value simply initialize the form with values, for example:

form = new FormGroup({
    name: new FormControl('defaultNameValue'),
    email: new FormControl('defaultEmailValue')
})
Sign up to request clarification or add additional context in comments.

Comments

1

You should be doing it in you component instead of using value attribute. While defining the form example

studentEditForm= new FormGroup({
    rollNo: new FormControl(this.student.rollNo)
})

or if student data is not available at the time of definition then use formcontrol.setValue after you initialize the student data something like

this.studentEditForm.controls.rollNo.setValue(this.student.rollNo)

Comments

1

Using the value and the CVA (Control Value Accessor: formControlName, formControl, and ngModel) to control the value of the input will override each other each time you change anyone of them.

Instead, it's better to rely only on one of them. If the CVA is used, then you can pass an initial value (default value) to the form-control while defining it:

studentEditForm = new FormGroup({
  score: new FormControl(YOUR_INITIAL_VALUE),
});

For more info, check here how Angular passes the value from CVA to the value property when using both of them: default_value_accessor.ts

Comments

1

Used value and CVA to control value

try this :

studentEditForm = new FormGroup({
   score: new FormControl(VALUE),
});

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.