In this video, learn how to use some of the built-in attribute directives to alter DOM elements.
- [Narrator] Attribute directives are designed to change the appearance or behavior of the DOM elements that they are attached to. They do not create or remove DOM elements like structural directives. Let's take a look at how you can use a built in attribute directive. In the media-item-list.component.html file we will make use of the ng-class attribute directive to get some CSS classes onto the media item element based on the media item medium type. Now the project already has CSS rules set up in the media-item.component.CSS file for some classes; so we can just start using those in a template. So on the mw-media-item element, we use the directive property template syntax using the square brackets around the term ngClass and set that equal to a statement. The ng-class directive expects an Object structure for it's statement value with the CSS classes for the property names and a true/false for the values. In our case, we want to have two CSS classes: one named 'medium-movies' and one named 'medium-series'. Since the dash is not valid in an Object property, we need to use single quotes around the term. We want to check the mediaItem.medium property to see if it is movies or series for each of these CSS classes and if true, then use it. We can do that with a statement that evaluates to true or false for each of these. So we set the value of medium-movies to mediaItem.medium triple equals and then movies, in single quotes; and we set the value of medium-series to mediaItem.medium triple equals, series in single quotes; and if we switch over to the browser, we see different colors for the media items; and if we inspect the DOM, we can see that the media items are getting the appropriate class applied to them from the ng-class attribute directive.
- What is Angular?
- Working with components
- Binding events and properties
- Getting data to components
- Using directives and pipes
- Creating Angular forms
- Validating form data
- How Angular does dependency injection
- Making HTTP calls
- Styling components
Skill Level Intermediate
Angular: Building an Interfacewith Ray Villalobos2h 20m Intermediate
1. Architecture Overview
3. Directives and Pipes
5. Dependency Injection and Services
8. Styling Components
Next steps1m 20s
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.