【问题标题】:Is there any way to loop through all properties in child component, binded in a parent component to a child in angular?有没有办法遍历子组件中的所有属性,在父组件中以角度绑定到子组件?
【发布时间】:2020-10-21 17:32:07
【问题描述】:
有什么方法可以遍历 child-component 中的所有属性,在 parent-component 中绑定到 angular 中的子组件。
例子:
在父组件中:
<app-child [a]="someval1" [b]="someval2" [c]="someval3" [d]="someval1"></app-child>
在子组件中:
@Input() // any function or callback which will loop through show all the child property binded in parent component
喜欢:
- a: someval1
- b: someval2 等等。
【问题讨论】:
标签:
javascript
angular
angular8
【解决方案1】:
我没有将其添加为评论的声誉-因此将其作为答案分享
与其尝试遍历组件的所有属性绑定,不如使用以下格式的单个属性绑定
someProp: {
a: 'someValue',
b: 'someOtherValue'
}
现在,在 parent 中你可以像这样使用它
<app-child [someProp]="obj" ></app-child>
一旦你以这种方式拥有它,在子组件中你可以轻松地使用 Object.keys(someProp)
【讨论】:
-
感谢好友的回复。实际上我的要求是在父母 ......... 和孩子组件我可以循环并获取绑定在父级中的所有属性到子级。然后在子组件的 html 中,我可以再次传递所有属性并绑定到其他组件,例如子组件 .