在Angular2中添加动态属性 [英] Add Dynamic Attributes in Angular2
本文介绍了在Angular2中添加动态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有现有的javascript函数,用于使用setAttribute函数创建动态元素。现在我想将属性[(ngModel)]附加到此元素中以进行双向数据绑定。我尝试使用
I have existing javascript function which is used to create dynamic element using setAttribute function. Now I want to append attributes [(ngModel)] into this element for two way data binding. I tried to add this using
obj.setAttribute([(ngModel)],modelName)
obj.setAttribute("[(ngModel)]", "modelName")
但我收到错误 -
Failed to execute 'setAttribute' on 'Element': '[(ngModel)]' is not a valid
推荐答案
不是从JavaScript设置属性,而是将所有属性放在数组中并循环它以呈现所有输入字段。
Instead of setting attribute from JavaScript, put all the properties inside array and loop it to render all the input fields.
<div ngFor="item in items">
<input [(ngModel)]="item.modelName" class="form-control" />
</div>
这篇关于在Angular2中添加动态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文