1

I have the following in the parent component:

output = {};

In the child I have this:

 @Input() output: GroupCompnent;
this.output.output = this.gridOptions.api.getSelectedRows();

Now I get this structure:

Object {output: Array(2)}
output Array(2)
[0]:Object
[1]:Object

But I want this:

[Object, Object]

Any suggestion how I can do that?

I want to achive two way data binding with object, so when I change value to child to be referenced on parent and get that data in parent.

Parent:

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

Child:

 @Input() output:GroupComponent;
 selected = () =>  this.output.output = this.gridOptions.api.getSelectedRows();

2 Answers 2

2

The output you are currently getting is totally correct. You are inserting an array to a property output in your object output. Seems that you want output to actually be an array. So change the declaration of output to an array in the parent:

output = [];

Then in parent, instead of just assigning the value as is, let's fetch the data and then push it to your output array. Also you shouldn't mark the @Input() with a component (?)

@Input() output:GroupComponent;

since it's an array:

@Input() output: Array<Object> // or if it's a typed array, change it

Then in OnInit we can fetch the array and map the content to the output array:

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

We need to do it this way to keep the reference between parent output and child output.

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

Comments

1

For a two way databinding you will need to declare an input and output in the child, something like this:

@Input() output= 0;
@Output() outputChange = EventEmitter<any>();

Have in mind that the output event has to be named like "Change"


After reading the comments I would suggest:

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

9 Comments

I dont need emit if i use complex object, that is two way binding automatically
Ok, I see the problem now. Could be that you need this.output = instead of this.output.output =
when i do this.output = .... in parent i dont get anything, i get an empty object again, or undefined
Any suggestion @kimy82 ?
I think I need more code. Could you paste the parent and child code.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.