Using @Input and @Output
Passing input into components, and sending events back out
PROModule Outline
- Source Code & Resources PRO
- Lesson 1: Introduction PUBLIC
- Lesson 2: The Role of Components and Directives PUBLIC
- Lesson 3: When to Use Custom Components & Directives PRO
- Lesson 4: Building a Simple Component PRO
- Lesson 5: Building a Simple Directive PRO
- Lesson 6: Setting up Components and Directives PRO
- Lesson 7: Using @Input and @Output PRO
- Lesson 8: Using ElementRef and Renderer PRO
- Lesson 9: Understanding Content Projection PRO
- Lesson 10: Understanding @ViewChild and @ContentChild PRO
- Lesson 11: Building a Complex Component PRO
- Lesson 12: Listening for Events and Binding to Properties PRO
- Lesson 13: Building a Skeleton Card Component PRO
- Lesson 14: Creating an Autogrow Directive for a Textarea PRO
- Lesson 15: Handling Error Messages with a Custom Component PRO
- Lesson 16: Building a Parallax Header Directive PRO
- Lesson 17: High Performance Accordion Component PRO
- Lesson 18: Conclusion PRO
Lesson Outline
Using @Input and @Output
At a basic level, a custom component is a great way to just modularise functionality in your application. Instead of coding some feature directly into a page, you can create it in a custom component instead and then include it where you want to use it:
<my-component></my-component>
This is what we did for the simple example we walked through in a previous lesson. We built a Motivator
component that we could just drop anywhere and it would allow the user to interact with a bunch of pre-defined quotes.
Although this is useful in itself, there are more complex ways that we can design our components. Rather than just having a static component, we might allow it to accept some input:
<my-cool-list [items]="items"></my-cool-list>
This would allow us to pass in some data to the MyCoolList
component, rather than having to hard code the data into the component. We might also want to detect when certain things happen inside of the component, and handle that outside of the component:
<my-cool-list (itemDeleted)="someFunction($event)"></my-cool-list>
This would allow us to listen for a custom itemDeleted
event emitted from the custom component, and we could then pass that event onto a function we define in the parent component.
Thanks for checking out the preview of this lesson!
You do not have the appropriate membership to view the full lesson. If you would like full access to this module you can view membership options (or log in if you are already have an appropriate membership).