Currently, I am developing an Angular application with a Model object that consists of properties of various types. My goal is to loop through these properties in the component and generate form fields for each property. Unfortunately, the implementation is not displaying anything.
Below is my code:
app.component.html:
<app-principal [model]="model" ></app-principal>
app.component.ts:
import { Component } from '@angular/core';
import { Model } from './model/model';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: './app.component.css'
})
export class AppComponent {
title = 'frontend';
model = new Model();
}
principal.component.ts:
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-principal',
templateUrl: './principal.component.html',
styleUrls: './principal.component.css'
})
export class PrincipalComponent {
@Input() model: any;
getModelProperties(): string[] {
return Object.keys(this.model);
}
}
principal.component.html:
<li *ngFor="let property of getModelProperties()">
<label for="{{ property }}">{{ property }}:</label>
<input type="text" [id]="property" [name]="property" placeholder="Write Something" [(ngModel)]="model[property]">
</li>
model.ts:
export class Model {
text!: string;
number!: number;
}