When to Use Custom Components & Directives
Using custom components too much? or not enough?
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
When to Use Custom Components & Directives
We know that we can create our own custom components and directives, but it may not be obvious when we should do that.
You can do a lot with Ionic and Angular without creating any of your own components or directives (except for the components we use to create the "pages" in our applications). We could just use a combination of the components that Ionic provides and plain HTML/CSS to build out the functionality of the application.
In the next lesson, we walk through creating a custom "motivator" component that allows a user to cycle through motivational quotes. We build that as a component, but we could just as easily build the functionality directly into the page rather than creating a custom component.
What situations call for a custom component and what situations call for a custom directive?
Custom Directives
A directive allows us to attach behaviour to existing elements in the application - so if we want to be able to modify the behaviour of elements in our application, we could create a custom directive to do that.
In another lesson, we will walk through a basic example of building a custom ClickDisappear
directive. This directive can be attached to any element in the application, and any element that it is attached to we can make disappear by clicking on it. We could implement this functionality easily enough without a directive - just set up a (click)
event binding on any element we want to disappear, and then set up a corresponding function in the host components class that will trigger hiding that element.
However, the benefit of using a directive, in this case, is reasonably obvious - we can remove a lot of the grunt work by creating a custom directive that we can reuse everywhere, then all we need to do to make an element disappear when we click it is add:
<some-element click-disappear></some-element>
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).