Angular 5 表单组列表控件或迭代它们

标签 angular forms web angular-fullstack formgroups

我在 formgorup 中使用 angular 5 并希望迭代控件,以便基于表单创建动态组件,表单字段由外部数据服务(数据库等)提供

它声明如下

check = new FormGroup({
    firstName : new FormControl(true),
    lastName : new FormControl(false)
  });

我找到了 this解释如何迭代控件,但它不起作用。我尝试使用:
for(let item of this.check.controls){}

并得到这是 chrome 调试:

Cannot read property 'length' of undefined



我无法访问 this.check.controls.keys 或 keys()

如何迭代 key ?

谢谢

最佳答案

尝试使用 Object.keys获取对象键的方法

Object.keys(this.check.controls); // => ["firstName", "lastName"]
this.check.controls如果要获取对象的键,则是对象键/值对结构{key1:value,key2:value}您可以使用 Object.keys 方法返回包含键的数组 ['key1','key2']

关于Angular 5 表单组列表控件或迭代它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51593705/

相关文章:

html - Chrome 不呈现 <form> 标签,FF 会

forms - Access 2003 表单边框的大小无法保持正确

css - Internet Explorer 11 和支持的网络字体

python - Python程序到网站应用程序

angular - 如何将 AWS CodeStar 用于 Angular 应用程序?

javascript - 将元素引用传递给指令

javascript - 显示需要用户名和密码的消息是什么?

linux - 为什么/var/www/需要755权限?为什么chown之后不是700?

Angular 2 在 Http.get 中使用 Observable.debounce()

html - Angular 2 - TypeError : Cannot read property 'apply' of undefined