Module 01- @HostBinding and @HostListener
February 1, 2023 ยท View on GitHub
DEMOS
- StackBlitz - @HostBinding and @Hostlistener
Training
Training 1: Use @HostBinding to change the background color of a host element
- Create a new Angular component called ColorChanger
- Add a public property called bgColor to the component with a default value of "white"
- Use the
@HostBindingdecorator to bind the bgColor property to the style
background-color property of the host element - Add a button to the component's template that when clicked, changes the value of bgColor to a random color
- Use the component in a parent template and observe the background color of the host element changing when the button is clicked.
Training 2: Using @HostListener to detect key presses
- Create a new Angular component called HoverEffect
- Add a public property called isHovered to the component with a default value.
- Use the
@HostBindingdecorator to bind the isHovered property to thebackground-colorproperty of the host element - Use the
@HostListenerdecorator to listen for the mouseenter and mouseleave events on the host element - In the event handler functions, set the isHovered property to true or false respectively
- Use the component in a parent template and observe the background color of the host element changing when the mouse enters and leaves the element.
BONUS: Creating a custom directive that interacts with other directives
- Create a new Angular directive called FancyTabs
- Use the
@Directivedecorator to apply the directive to thenavelements with a specific CSS class in the application - Use the
@ViewChildrendecorator or other Angular techniques likeElementRefto get access to all theaelements inside the host element - Add a property called activeTab to the directive with a default value of 0
- Use the
@HostListenerdecorator to listen for theclickevent on theaelements. In the event handler function, set the activeTab property to the index of the clicked element - Use the
@HostBindingdecorator to bind the activeTab property to the border-bottom property of the host element. When activeTab is set to a specific index, the border-bottom of the correspondingaelement should be2px solid purple - Use the directive in a parent template and observe the border-bottom of the
aelements changing when they are clicked.
๐ Info: The
@HostBindingand@HostListenerdecorators are used to bind to the host element of a component. This is useful when you want to change the host element's properties or listen to events on the host element. The@HostBindingdecorator is used to bind to the host element's properties and the@HostListenerdecorator is used to listen to events on the host element.
:warning: ATTENTION: Note that this are simplified examples, in a real application you might want to use
ngClassorngStyleto change the style of the element, instead of using@HostBindingwith the style property of the host element.
๐ฐ Summary
@HostBindingand@HostListenerare used to bind to the host element of a component