I have this component that receives two inputs via its selector, but this can be extended to any number of inputs and any component. So, in quest of consuming multiple properties in the component itself a single @Input()
decorator did not allow me to consume multiple properties, so as a workaround I used two decorators for two input properties but I don't think this would be the only way to tackle such a scenario.
export class AsyncComponent {
@Input() waitFor: boolean;
@Input() message: string; // TODO: Verify if multiple inputs are needed for multiple props
}
Update
<app-async [waitFor]="true" message="foo"><app-async>
So, is is possible with any other way to just use a single decorator for any number of input props? If there are more props that I am passing other than waitFor
and message
would I have to do the following for each prop.
@Input() waitFor: boolean;
@Input() message: string;
@Input() someOtherProp: string;
....
Or is there any other way to just have one Input
decorator and consume any number of properties?
The <parent-component> serves as the context for the <child-component> . @Input() and @Output() give a child component a way to communicate with its parent component. @Input() lets a parent component update data in the child component. Conversely, @Output() lets the child send data to a parent component.
Use the @Input() decorator in a child component or directive to let Angular know that a property in that component can receive its value from its parent component. It helps to remember that the data flow is from the perspective of the child component.
@Input is a decorator to mark a property as an input. @Input is used to define an input property, to achieve component property binding. @Inoput decorator is used to pass data (property binding) from parent to child component. The component property should be annotated with @Input decorator to act as input property.
Input ( @Input() ) is one of the most used decorators in Angular apps. It is used to pass data from the parent or host component to the child component. This decorator has a relation with DOM property in the template where the child component is used.
I agree with Roman C.
I would just pass a single object (not array) containing all props:
@Component({
selector: 'app-async'
})
export class AsyncComponent {
// Single object with all props.
@Input() props: { waitFor: boolean; message: string; };
}
And then the parent component:
@Component({
template: '<app-async [props]="myProps"><app-async>'
})
export class ParentComponent {
myProps = { waitFor: true, message: 'foo' };
}
NB. Be aware that interfaces declared on input properties are NOT ENFORCED. It is best practice to still declare them, but JavaScript can't runtime-check a TypeScript interface. This remark applies to all code samples in this thread (single input, multiple inputs...).
It's not possible if you need multiple values use an array or something
export class AsyncComponent {
@Input() props: any[];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With