Templates, Components & Directives
Angular's templating layer starts with spec-compliant HTML, and allows to add dynamic template expressions, declarative components and DOM transformations and more! We'll take a close look at pipes, attribute and structural directives -- highlighting appropriate and practical use cases along the way.
-
Templates, Components & DirectivesTemplates Expressions & Pipes
Angular's declarative template layer allows us to express the HTML structure of components using familiar concepts. Mustache-like template expressions make working with simple values easy, and pipes allow us to pass values through one or more functions for formatting or transformation. We'll study how these concepts work, and highlight techniques for recognizing problems that are best solved by pipes.
-
Templates, Components & DirectivesExercise: Nice Number
We'll build a number formatting pipe that allows us to take difficult-to-read numbers like 1492821412 and represent them as strings like "1.49B". Through handling various edge and corner cases, providing optional arguments for padding zeroes and controlling decimal precision, we'll end up with a robust pipe that's broadly useful across an angular app.
-
Templates, Components & DirectivesStateful Pipes
One of the most interesting pipes that comes for free with Angular is
async
-- allowing a Promise or Observable to be used in templates without any further ceremony. We'll look at the concept of an "impure pipe", and how through using a ChangeDetectorRef, we can build pipes that trigger view updates on their own terms. -
Templates, Components & DirectivesExercise: Show In 5...
We'll build a pipe that reveals any value after 5 seconds have elapsed (and counts down time for users), taking care to avoid memory leaks, and handle cases where the value is no longer on the screen once this time has elapsed.
-
Templates, Components & DirectivesAttribute Directives
Attribute directives allow us to customize the appearance or behavior of DOM elements or custom components, simply by adding regular HTML attributes. We'll start with the "hello world" of attribute directives, highlighting an element, and progressively introduce new features and options to illustrate what's possible.
-
Templates, Components & DirectivesExercise: Easy Placeholder Images
We'll build an attribute directive that can be applied to IMG tags, setting their src attribute to a placeholder image URL of the specified dimensions. This allows us to build UI layouts quickly and easily, simply by specifying the width and height of images, and tagging them with a "appPlaceholder" HTML attribute.
-
Templates, Components & DirectivesExercise: DOM Event Logger
Create an appLogEvent attribute directive, that adds event listeners to the host DOM element, logging out events whenever they occur.
-
Templates, Components & DirectivesStructural Directives
Structural directives allow us to transform a template's HTML by adding, removing or changing elements. We'll take a careful look at some that come for free with Angular like NgFor, NgIf and NgSwitch, and then take the first steps toward building a custom structural directive of our own.
-
Templates, Components & DirectivesExercise: Reval For Role
Build a structural directive, leveraging an existing auth.service to mark certain pieces of UI to be shown only to users with the appropriate roles
-
Templates, Components & DirectivesWrap up and recap
We'll recap everything we've covered throughout the day, and set our sights on tomorrow's agenda.