angular · Angular Basics
In Angular, you can pass data from the parent component to the child component by using:
Answers
- @Output()
- @Input()
- Input
- Output
{{childData}}
` }) export class ChildComponent { @Input() childData: string; } ``` In the example above, we are using the `@Input()` decorator to bind the 'childData' property in the child component to receive data ('parentData') from its parent component. On the other hand, using `@Output()` is incorrect for this scenario because `@Output()` is designed for the opposite case - it allows the child component to emit events to the parent component, not to receive data. The 'Input' and 'Output' without the '@' symbol are also incorrect because in Angular, they don’t carry any special significance and would just be treated as standard properties. The best practice when using `@Input()` in Angular is to declare it within the child component where the value is required. It is also considered a good practice to keep track of changes to the Input property by using the `OnChanges` lifecycle hook in the child component. The `@Input()` thus acts as an effective and recommended method to pass data from parent component to child component in Angular, ensuring seamless data sharing and boosting the robustness of Angular applications.