Angular binding object with array from component to view with ngModels
The problem is, whatever the input
name specified, it will be considered as just text. Should not reference any Object
or Array
.But with slight modifications, there is a chance to obtain the same data structure with updated data.
All elements are binded in one way, so make it two way binding. So whenever input
value changes, The binded element
value updates.
[(ngModel)]="question.question"
Use template driven form
for validation
<form #form="ngForm" (ngSubmit)="createQuestion()" class="mt-4">
...
<button [disabled]="!form.valid"
Now the values are validated & updated in the variable question
.There is no need to get the value from form, use the updated object question
to create question.
createQuestion() {
console.log(this.question);
}
View the results in worked out App