Angular 与对象的双向数据绑定?

Angular 与对象的双向数据绑定?,angular,data-binding,Angular,Data Binding,我在父组件中有以下内容: output = {}; 在孩子身上,我有这样一个想法: @Input() output: GroupCompnent; this.output.output = this.gridOptions.api.getSelectedRows(); 现在我得到这个结构: Object {output: Array(2)} output Array(2) [0]:Object [1]:Object 但我想要这个: [Object, Object] 有什么建议吗 我想实

我在父组件中有以下内容:

output = {};
在孩子身上,我有这样一个想法:

 @Input() output: GroupCompnent;
this.output.output = this.gridOptions.api.getSelectedRows();
现在我得到这个结构:

Object {output: Array(2)}
output Array(2)
[0]:Object
[1]:Object
但我想要这个:

[Object, Object]
有什么建议吗

我想实现与对象的双向数据绑定,所以当我将值更改为child时,将在父对象上引用该值,并在父对象中获取该数据

家长:

  output = {};
   <div *ngIf="visible">
     <z-ag-table [items]="gridOptions" [output]="output"></z-ag-table>
    </div>

对于双向数据绑定,您需要在子对象中声明输入和输出,如下所示:

@Input() output= 0;
@Output() outputChange = EventEmitter<any>();
@Input()输出=0;
@Output()outputChange=EventEmitter();
请记住,输出事件必须命名为“Change”


在阅读评论之后,我建议:

selected = () =>  { this.output.splice(0); 
     this.output.concat(<your new array>(this.gridOptions.api.getSelectedRows()))
}
selected=()=>{this.output.splice(0);
this.output.concat((this.gridOptions.api.getSelectedRows())
}

您当前获得的输出完全正确。您正在将数组插入对象的属性
输出
。似乎希望
输出
实际上是一个数组。因此,将
output
的声明更改为父级中的数组:

output=[];
然后在parent中,我们获取数据,然后将其推送到
输出
数组,而不是按原样分配值。此外,您不应使用组件(?)标记
@Input()

@Input()输出:GroupComponent;
因为它是一个数组:

@Input()output:Array//或者如果它是类型化数组,请更改它
然后在
OnInit
中,我们可以获取数组并将内容映射到
输出
数组:

ngOnInit(){
让arr=this.gridOptions.api.getSelectedRows()
arr.map(x=>this.output.push(x))
}

我们需要这样做,以保持父对象
输出
和子对象之间的引用
输出

如果使用复杂对象,我不需要发射,这是双向绑定自动Yok,我现在看到问题了。当我这样做时,可能需要这个.output=而不是这个.output.output=。。。。在parent中,我什么都没有得到,我又得到了一个空对象,或者是一个不确定的建议@kimy82?我想我需要更多的代码。你能粘贴父代码和子代码吗。
selected = () =>  { this.output.splice(0); 
     this.output.concat(<your new array>(this.gridOptions.api.getSelectedRows()))
}