简体   繁体   English

Angular2 @Input:将对象数组从父组件传递到子组件

[英]Angular2 @Input : Passing an objects array from parent to child component

I need some help with angular 2. Is it possible to pass an objects array from parent to child component? 我需要有关angular 2的帮助。是否可以将对象数组从父组件传递到子组件? It seems its not possible, But maybe i missed something. 似乎不可能,但是也许我错过了一些东西。 Below you could see a summary what ive been trying in my code. 在下面,您可以看到我在我的代码中尝试过的摘要。

parent.component.ts
-----------------------------------------

@Component({
    template: `
        <child dataset="{{ people }}"></child>  
    `,
})

export class ParentComponent{

    private people: any[] = [
        { name: 'jimmy', age: 22, gender: 'male' },
        { name: 'johnny', age: 23, gender: 'male' },
        { name: 'danny', age: 24, gender: 'male' }
    ];

}


child.component.ts
-----------------------------------------
export class ChildComponent implements OnInit{

@Input() private dataset: any[] = [];

    ngOnInit() {
        console.log(this.dataset);
    }  

}


console
-----------------------------------------
[object Object],[object Object],[object Object]

The interpolation like dataset="{{ people }}"> is always stringified while the value of basic property binding [dataset]="people" is passed as is. 当基本属性绑定[dataset]="people"值按原样传递时,像dataset="{{ people }}">这样的插值始终是字符串化的。

So you need to replace interpolation with: 因此,您需要将插值替换为:

[dataset]="people"

See also 也可以看看

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM