3

I have a component with input of type array

class FooComponent  {  
@Input() selectedItemIds:String[];
}

and I would like to use map expression in binding in the parent component

<app-foo-component [selectedItemIds]='items.map(i=>i.Id)'><app-foo-component>

and I get the nice angular error

Bindings cannot contain assignments...

so what is the solution ?

**Note: I know how to do it in component class.I want to know if it is somehow possible via template and the code is very brief, I just wanted to show what I was trying to do **

3
  • Can you map your items in the *.ts file before you try to pass them to the component? Commented Nov 4, 2017 at 12:48
  • items are in ts file , it is an array of object Commented Nov 4, 2017 at 12:48
  • In other words, you can't do this mapping inside the binding. You need to have prepared the array before trying to pass it. If you're trying to do it this way because your component is being built before your array has finished then put a *ngIf on <app-foo-component... to force it to run only when your array is ready. Commented Nov 4, 2017 at 12:51

1 Answer 1

1

Call the map function in the *.ts file before you try to pass it to <app-foo-component>. If you're trying to do it this way because your component is being built before your array has finished then create some property hold it.

<app-foo-component *ngIf="itemsReady" [selectedItemIds]='items'><app-foo-component>

Then in your *.ts file you can create some function to do your mapping

itemsReady = false;

mapFunction() {
  // do your mapping and when it's complete set this.itemsReady = true
} 
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.