【问题标题】:Angular2 @Input : Passing an objects array from parent to child componentAngular2 @Input:将对象数组从父组件传递到子组件
【发布时间】:2016-12-16 19:31:22
【问题描述】:

我需要一些有关角度 2 的帮助。是否可以将对象数组从父组件传递到子组件?这似乎是不可能的,但也许我错过了一些东西。您可以在下面看到我在代码中尝试的摘要。

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]

【问题讨论】:

  • 它似乎有效,不是吗?究竟是什么问题?
  • 插值将表达式转换为字符串。使用属性绑定,如[dataset]="people"
  • 感谢:[dataset]="people" 有效!
  • @yurzui 嗨,听起来很酷。如果您可以在答案部分回答问题,那就太好了,这样问题就会显示为带有解决方案的答案。

标签: angular angular2-template


【解决方案1】:

dataset="{{ people }}"&gt; 这样的插值总是被字符串化,而基本属性绑定[dataset]="people" 的值按原样传递。

所以你需要将插值替换为:

[dataset]="people"

另见

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    • 2016-05-10
    • 1970-01-01
    • 2018-05-01
    • 2018-11-26
    相关资源
    最近更新 更多