since I got a form with a lot of different input fields, to avoid having a gigantic html template I want to put the fields data inside objects and create the template dynamically using ngFor.
This is my code until now.
stub.ts - this is where I store my data
export const MY_DATA = [
{ placeholder: 'First name', name: 'name', model: 'model.name'},
{ placeholder: 'Last name', name: 'lastName', model: 'model.lastName'},
{ placeholder: 'Age', name: 'age', model: 'model.age'}
];
component.ts
import { Component, OnInit } from '@angular/core';
import {MY_DATA} from './stub';
@Component({
selector: 'app-valutazione-insert',
templateUrl: './valutazione-insert.component.html',
styleUrls: ['./valutazione-insert.component.css']
})
export class ValutazioneInsertComponent implements OnInit {
model: any = {};
data = MY_DATA;
constructor() { }
ngOnInit() {}
submit() {
console.log('Data submitted: ', this.model);
}
}
template.html
<div id="datiRichiesta" [hidden]="datiRichiestaClicked" >
<div class="form-group" *ngFor="let d of data">
<input type="text" class="form-control" placeholder="{{d.placeholder}}" [(ngModel)]=d.model name="{{d.name}}"
onfocus="this.placeholder = ''" (blur)="this.placeholder = d.placeholder"/>
</div>
</div>
These are the input fields I got:

This is the rendered html code:

Now, I got some things that don't work:
- even if on the rendered html all attributes are correctly set, the placeholder value takes the model value
- the behaviour of the placeholder
onfocusandonbluris not working - most important, if I try to submit some values these are not passed to the controller
Now every problem disappear if I use an input type with all properties set statically. Is there any way to achieve what I want?
Thanks!