2

I need some help with angular 2. Is it possible to pass an objects array from parent to child component? 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]
4
  • It seems to work, no? What is actually the problem? Commented Aug 10, 2016 at 12:06
  • 4
    Interpolation converts expression to string. Use property binding like [dataset]="people" Commented Aug 10, 2016 at 12:11
  • Thanks: [dataset]="people" works! Commented Aug 10, 2016 at 12:24
  • @yurzui Hi, That sounds cool. would be great if you can answer the question in the answer section so that question will appear as answered with a solution. Commented Jan 31, 2017 at 6:36

1 Answer 1

5

The interpolation like dataset="{{ people }}"> is always stringified while the value of basic property binding [dataset]="people" is passed as is.

So you need to replace interpolation with:

[dataset]="people"

See also

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.