I want to insert dynamically attributes to an input html tag, but I don't know to to do this:
I've got this code from component side:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-transclusion',
templateUrl: './transclusion.component.html',
styleUrls: ['./transclusion.component.css']
})
export class TransclusionComponent implements OnInit {
elements: any;
constructor() { }
ngOnInit() {
this.elements = {};
this.elements.name = 'TEST1';
this.elements.type = 'text';
this.elements.value = '12';
this.elements.placeholder = 'PRUEBA';
this.elements.maxlength = '10';
// This is only for test elements keys
for (const el in this.elements) {
if (this.elements.hasOwnProperty(el)) {
console.log(`${el}: ${this.elements[el]}`);
}
}
}
}
And this is my template side:
<input type="text"
[attr.name]="elements.name"
[attr.value]="elements.value"
[attr.placeholder]="elements.placeholder"
[attr.maxlength]="elements.maxlength"/>
I want any 'forin' like method to iterate over each elements attribute and insert dynamically on the input tag, so it results like this:
<input type="text"
[attr.*for="el in elements"]="el"/>
How can I implement this?
Best Regards Antonio