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 Goal
Create an Angular Structural Directive to insert an element and immediately hide the element.
Tasks
- Open the if-and-hide.directive.ts file located in the src/app/directives directory.
- Define a new setter method for the
appIfAndHide
property within theIfAndHideDirective
class. When the value of the property is set, this function will be invoked with a booleancondition
value specified as the first, and only, argument. - Apply the
@Input()
decorator to the setter method you just created for theappIfAndHide
property. - Within the setter method, invoke the
create()
method if the value of thecondition
argument istrue
, else, invoke theremove()
method. - Next, we need to inject a few classes via the
constructor()
function that our directive requires, namely:Renderer2
,TemplateRef
, andViewContainerRef
. - Within the
create()
method invoke thecreateEmbeddedView()
method on the injectedViewContainerRef
class instance, providing theTemplateRef
instance. Iterate over therootNodes
property of the embedded view, and for each node, set thedisplay
CSS style to the valuenone
. - Then, within the
remove()
method invoke theclear()
method on theViewContainerRef
class instance in order to destroy all views in the container. - Finally, test your solution using the Developer Tools inspection to note that the
<div>
element is created, but thedisplay
style is set tonone
when the value of theloading
boolean property in theAppComponent
class is set totrue
.
Exercise
Notes
- Be sure to use the Inspect developer tools within the browser to monitor the result of the
<div>
in the app.component.html template on which theNgIfAndHide
structural directive is applied. - The
Renderer2
,TemplateRef
, andViewContainerRef
class instances are already imported at the top of the if-and-hide.directive.ts file, but don't forget to inject them via dependency injection in theIfAndHideDirective
class'sconstructor()
function. - You may want to refer to the https://angular.io/api/core/ViewContainerRef#viewcontainerref documentation for the behavior of both the
createEmbeddedView()
andclear()
methods.