I have https://stackblitz.com/edit/ingecalc?file=src/app/app.component.ts
paramsFormArray: FormArray;
properties: Record<string, number> = { a: 1, b: 2 };
constructor(private fb: FormBuilder) {
this.paramsFormArray = this.fb.group(
this.properties,
);
}
Now I want that for each property
in properties
to have an input
in the HTML.
a = [1 ]
b = [2 ]
c = ...
d = ...
However it does not work. I tried also "array" instead of "group" does not work either...
There is a lot of things going wrong. So Ill give my level best to explain, please ask any doubts if you have any.
Calculator needs to be a service ( basically the same as class, but is accessible through constructor, like how you did, class cannot be accessed through constructor ( through Dependency Injection)
Form Group needs to be initialized and we can subscribe to an observable of formgroup called
valueChanges
this will trigger whenever the value gets changed.In HTML we can wrap the controls inside a form and add the attribute
[formGroup]="formGroup"
this will map the created form group to the controls set in HTML.we do not need
ngModel
orkeyDown
since we subscribe to the changes!component details
service details
stackblitz