Outline
- Fundamentals of Angular Directives - What is Modularity?
- Fundamentals of Angular Directives - Three Types of Directives in Angular
-
Fundamentals of Angular Directives - Attribute Directives
- Introduction
- Attribute Directives API
- ElementRef Exercise
- ElementRef Exercise Solution
- Renderer2 Exercise
- Renderer2 Exercise Solution
- Building our First Attribute Directive
- Building Actions Attribute Directive Exercise
- Actions Attribute Directive Exercise Solution
- Host Decorators
- HostBinding Exercise
- HostBinding Exercise Solution
- HostListener Exercise
- HostListener Exercise Solution
- Directive Inputs
- Directive Inputs Exercise
- Directive Inputs Exercise Solution
- Input, HostBinding and HostListener Exercise
- Input, HostBinding, and HostListener Exercise Solution
- BONUS: Mos Eisley Cantina Exercise
- Mos Eisley Cantina Exercise Solution
-
Fundamentals of Angular Directives - Structural Directives
- Introduction
- Built-in Structural Directives
- NgIf Built-in Structural Directive
- NgIf Structural Directive Exercise
- NgIf Exercise Solution
- NgFor Built-in Structural Directive
- NgFor Structural Directive Exericse
- NgFor Exercise Solution
- NgSwitch Built-in Structural Directive
- NgSwitch Structural Directive Exercise
- NgSwitch Exercise Solution
- NgSwitch with Enum
- NgSwitch with Enum Exercise
- NgSwitch with Enum Exercise Solution
- Structural Directives API
- FadeOut Structural Directive
- IfAndHide Structural Directive Exercise
- IfAndHide Directive Exercise Solution
-
Fundamentals of Angular Directives - Component Directives
- Introduction
- Component Templates
- Component Property Binding Exercise
- Component Property Binding Exercise Solution
- Component Event Binding Exercise
- Component Event Binding Exercise Solution
- Component Bindings
- Component Directive Exercise
- Component Directive Exercise Solution
- Component Output Binding Exercise
- Component Output Binding Exercise Solution
- Container and Presenter Component Design
- Modular Component Design Exercise
- Modular Component Design Exercise Solution
- Thank You
Outline
- Fundamentals of Angular Directives - What is Modularity?
- Fundamentals of Angular Directives - Three Types of Directives in Angular
-
Fundamentals of Angular Directives - Attribute Directives
- Introduction
- Attribute Directives API
- ElementRef Exercise
- ElementRef Exercise Solution
- Renderer2 Exercise
- Renderer2 Exercise Solution
- Building our First Attribute Directive
- Building Actions Attribute Directive Exercise
- Actions Attribute Directive Exercise Solution
- Host Decorators
- HostBinding Exercise
- HostBinding Exercise Solution
- HostListener Exercise
- HostListener Exercise Solution
- Directive Inputs
- Directive Inputs Exercise
- Directive Inputs Exercise Solution
- Input, HostBinding and HostListener Exercise
- Input, HostBinding, and HostListener Exercise Solution
- BONUS: Mos Eisley Cantina Exercise
- Mos Eisley Cantina Exercise Solution
-
Fundamentals of Angular Directives - Structural Directives
- Introduction
- Built-in Structural Directives
- NgIf Built-in Structural Directive
- NgIf Structural Directive Exercise
- NgIf Exercise Solution
- NgFor Built-in Structural Directive
- NgFor Structural Directive Exericse
- NgFor Exercise Solution
- NgSwitch Built-in Structural Directive
- NgSwitch Structural Directive Exercise
- NgSwitch Exercise Solution
- NgSwitch with Enum
- NgSwitch with Enum Exercise
- NgSwitch with Enum Exercise Solution
- Structural Directives API
- FadeOut Structural Directive
- IfAndHide Structural Directive Exercise
- IfAndHide Directive Exercise Solution
-
Fundamentals of Angular Directives - Component Directives
- Introduction
- Component Templates
- Component Property Binding Exercise
- Component Property Binding Exercise Solution
- Component Event Binding Exercise
- Component Event Binding Exercise Solution
- Component Bindings
- Component Directive Exercise
- Component Directive Exercise Solution
- Component Output Binding Exercise
- Component Output Binding Exercise Solution
- Container and Presenter Component Design
- Modular Component Design Exercise
- Modular Component Design Exercise Solution
- Thank You
Exercise Goals
Combine your skills of using the directive @Input()
, @HostBinding()
, and @HostListener()
decorators in Angular Directives.
Tasks
- Open the actions.directive.ts file and define a new setter method for the
position
property. First, verify that theel
class instance property value is not a falsey value, as this would indicate that thenativeElement
of theElementRef
is not available, and as such, we are not executing in the context of a browser. Then, set thejustifyContent
CSS property value of the host element (hint, usethis.el
) toflex-start
if the value specified to the setter is "start", else, set thejustifyContent
CSS property value of the host element toflex-end
if the value specified to the setter is "end". - Apply the
@HostBinding()
decorator to theactions
property in theActionsDirective
class, binding to theclassList
property of the host element and adding the "actions" class name. - Open the btn-grow.directive.ts file and add the
@HostBinding()
decorator tobtnGrow
property in theBtnGrowDirective
class, binding to theclassList
property of the host element and adding the "btn-grow" class name. - Add the
@HostListener()
decorator to theonMouseOver()
andonMouseOut()
methods in theBtnGrowDirective
class specifying the appropriate event to invoke each method.
Exercise
Notes
- If you are unfamiliar with a setter method, it a method that is invoked when a property value is set. Use the syntax:
set position(value: string) {}
to specify the setter method for theposition
property. The argument can be called anything. I've simply called itvalue
in this example. - The
ElementRef
andRenderer2
class instances are already injected via the class'sconstructor()
function for you. - Don't forget that Angular provides syntactic sugar for modifying a host element's
classList
property. Use the stringclass.class-name
as thehostPropertyName
argument value to theHostBinding()
decorator function. - When specifying the event using the
HostListener()
decorator just specify the event name, omit the "on" prefix if you are used to attaching event listeners in HTML templates. - Open the app.component.html file and note that the
appActions
andappBtnGrow
attribute directives are applied to the<div>
element on line 9 and the<button>
elements on lines 10 and 11. - Try modifying the
position
attribute value on the<div>
element on line 9 in app.component.html to "start" to verify that the justification is working correctly.