1

I would like to send multiple objects or parameters to child component using one input :

<child [OneInput]="{object1,object2}"></child>

Because in child component im usnig set method to get data from parent component

@Input()
set OneInput(data)
{
 console.log(data)
}

i don't want to create a separated variable kind let obj= {obj1:data1,obj2:data2}

Any help please.

2
  • Create object on the .ts let object = {object1,object2} and then used that variable on view as <child [OneInput]="object"></child> Commented Dec 6, 2018 at 9:11
  • is there any possibility to do it without touching ts file ? Commented Dec 6, 2018 at 9:12

1 Answer 1

2

If on your component, you specify 2 variables that you want to pass in your template then you can directly pass it on [OneInput]

ParentComponent

@Component({
   ...,
   template: `<child [OneInput]="{userList: users, positionList: positions}"></child>`
})
export class ParentComponent {

   users = ['user1', 'user2'];
   positions = ['position1', 'position2'];

}

ChildComponent

@Component({...})
export class ChildComponent {

   @Input()
   set OneInput({userList, positionList}) {    // You can use data or you can destructure it to directly access the objects without having to data.userList and data.positionList
      console.log(userList, positionList)
   }

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

Comments

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.