Component Outputs
Part of the Fundamentals section of Coddy's Angular journey. Lesson 28 of 55.
A child reports an action with an output event. Create an EventEmitter field marked with Output, then emit a payload when the action occurs. The parent handles that event using its own binding.
// Child field: @Output() picked=new EventEmitter<string>();
// Child button: <button (click)="picked.emit('Blue')">Pick</button>
// Parent: <color-card (picked)="selected.set($event)"></color-card>The parent receives the emitted payload as $event. It can update its own state without the child reaching into parent fields. Use Output to declare the event and EventEmitter to send its payload.
Challenge
MediumHave ChoiceCard emit its drink choice through picked. The parent must display the latest payload in selected, initially None. Clicking Tea displays Tea.
Edit the supplied files. Press Run preview to refresh the preview after edits, then use Check or Run code to grade your work. Keep the requested element IDs.
Try it yourself
<!doctype html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="stylesheet" href="styles.css"></head><body><app-root></app-root><script type="module" src="main.ts"></script></body></html>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
1Meet Angular
Angular in ActionA Standalone ComponentComponent FieldsTypeScript in ComponentsRecap: Meet Angular7Forms and Validation
Two-Way BindingNormalizing Form TextSubmitting a FormValidation GuardsRecap: Forms2Template Bindings
Text InterpolationProperty BindingClass BindingStyle BindingRecap: Template Bindings5Conditions and Lists
Conditional ViewsRendering ListsEmpty List ViewsFiltering ViewsRecap: Control Flow6Reusable Components
Child ComponentsComponent InputsComponent OutputsContent ProjectionRecap: Components9Services and Shared State
Injecting a ServiceSharing Service StateService CommandsDerived Service StateRecap: Shared State