我的密码里有这个。
@Component({
selector: 'generic-input',
template: `<div><input [formControl]="control"/></div>`,
})
export class GenericInputComponent implements OnInit {
@Input('config') config = {placeholder: 'Testability', disabled: true, type: 'text'};
control;
constructor() { }
ngOnInit() {
this.control = new FormControl();
}
}我希望通过使用某种循环或其他方法扩展config对象中的属性,以便呈现的html如下所示:
<div><input placeholder='Testability', disabled=true type='text' [formControl]="control"/></div>备注:以下不是一个选项:
<div><input [placeholder]='config.placeholder', [disabled]='config.disabled' [formControl]="control"/></div>欢迎任何帮助和想法。谢谢。
发布于 2018-12-19 05:38:01
1.添加一个模板变量:
<div><input #v [formControl]="control"/></div>2.在ts中绑定ViewChild:
@ViewChild('v')
v: ElementRef3.在其nativeElement属性中填充值:
const el = this.v.nativeElement
Object.keys(this.config).forEach(key => el[key] = this.config[key])更新
完整的component.ts文件如下所示:
@Component({
selector: 'my-app',
template: `<div><input #v></div>`
})
export class AppComponent {
@ViewChild('v')
v: ElementRef;
config = { placeholder: 'Testability', disabled: true, type: 'text' };
ngOnInit() {
const el = this.v.nativeElement;
Object.keys(this.config).forEach(key => el[key] = this.config[key]);
}
}https://stackoverflow.com/questions/53845004
复制相似问题