简体   繁体   English

如何将 Object 从父模板传递给子组件

[英]How can i pass an Object from my Parent template to the Child Component

i have a interface which looks like this我有一个看起来像这样的界面

export interface IDropdownOption {
  guid: string;
  width: string;
  isRequired: false;
  fieldText: string;
  selectedItem: string;
}

In my Child component i define an Input like this @Input() compInfo: IDropdownOption;在我的子组件中,我定义了一个像这样的输入@Input() compInfo: IDropdownOption; and then in my Parent template i use the client like this然后在我的父模板中我使用这样的客户端

<app-ig-dropdown
        compInfo.guid="820E04E0-8084-4D9C-A268-D8C0D21E74F6"
        compInfo.width="200px"
        formControlName="combo1"
        compInfo.fieldText="Social Media">
</app-ig-dropdown>

Doing so will always generate me errors that the property of any of these values defined in object compInfo are undefined.这样做总是会产生错误,即 object compInfo 中定义的任何这些值的属性未定义。 Is it even possible to use an Object as the @input and then set the values from the parent template?甚至可以使用 Object 作为@input,然后从父模板设置值吗? I am trying to avoid declaring 50 + inputs as i might have 10 child components on the parent which each has 5 param.我试图避免声明 50 + 输入,因为我可能在父组件上有 10 个子组件,每个子组件有 5 个参数。 If this is not supported how else can i make this more clean?如果这不受支持,我还能如何让它更干净?

Ultimately i was able to solve the issue by assigning the values in my parent template like this.最终,我能够通过像这样在我的父模板中分配值来解决这个问题。

<app-ig-dropdown
       [compInfo]="{guid :'820E04E0-8084-4D9C-A268-D8C0D21E74F6',
                    width:'350px',
                    placeHolder: ' -- Select --',
                    fieldText:'Social Media 1'}"
        formControlName="combo1"
        >
</app-ig-dropdown>
<app-ig-dropdown
        [compInfo]="{guid :'820E04E0-8084-4D9C-A268-D8C0D21E74F6',
                    width:'350px',
                    placeHolder: ' -- Select --',
                    fieldText:'Social Media 2'}"
        formControlName="combo2"
>
</app-ig-dropdown>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据从子组件传递到父组件? - How can I pass data from child component to parent component? 如何将模板从父组件传递到子组件并保存执行上下文? - How to pass template from parent to child component and save context of execution? 如何通过父组件将表单传递给子组件(MobileItems 到 Accessories Items)? - How can I pass my form by parent component to child component (MobileItems to Accessories Items)? 如何在Angular 2中将对象从“父”组件传递到“子”组件? - How can I pass the objects from Parent component to the Child component in Angular 2? Angular:我可以将值从控制台传递给组件,就像父组件如何将值传递给它的子组件一样? - Angular: Can I pass values from the console to a component like how a parent component passes a value to its child? 如何将 object 从父组件传递到子组件作为参考 - How to pass object from parent to child component as reference 如何将子组件的 formGroup 传递给父组件 - How can i pass the formGroup of a child components to Parent Component 如何使用单击事件从父组件中的子组件刷新我的表格内容? - How can I refresh my table content from the child component in the parent component with click event? 如何从不嵌套的父组件传递到子组件? - How do I pass from parent to child component which is not nested? 如何将 $event object 从我的模板传递到 Angular 上的组件? - How to pass $event object from my template to my component on Angular?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM