Need to pass data from one component to another component

Viewed 41

I use @Input() to pass an ID from another component, but I initialize the ID with a number like this:

@Input() ServiceId: number=21;

How can I initialize the input with that number from the initial component? Do I directly assign a variable to @Input()?

1 Answers

You send the data to the child component via the parent component's template.

For your case, your parent component's template could look something like this:

<my-sub-component [ServiceId]="21"></my-sub-component>

If your parent component has a property called, e.g., myServiceId, you can pass that in as well:

<my-sub-component [ServiceId]="myServiceId"></my-sub-component>

Take a look at the documentation here for more information.

Related