- Overview
- Transcript
4.8 The `*ngFor` Directive
The *ngFor
directive is a repeater that allows us to duplicate a block of markup for each item in an array. This allows us to create dynamic, data-driven views.
1.Introduction6 lessons, 42:00
1.1Introduction00:48
1.2Get Started With Angular-CLI11:09
1.3Developing With Angular-CLI13:17
1.4TypeScript vs. JavaScript06:54
1.5Angular Modules From the CLI04:31
1.6CLI Options05:21
2.Get Started With Angular7 lessons, 42:38
2.1Bootstrapping the Application04:30
2.2The Application Module04:15
2.3The Application Component08:06
2.4Component Styling03:06
2.5Global Styling05:11
2.6Creating a Component With the CLI09:34
2.7Creating a Service With the CLI07:56
3.Core Concepts7 lessons, 55:20
3.1Component Trees06:20
3.2Dependency Injection06:52
3.3Content Projection05:38
3.4Component and Directive Lifecycle Methods06:31
3.5Component-Only Lifecycle Methods05:28
3.6Decorators07:36
3.7Models16:55
4.Template Deep Dive11 lessons, 1:10:56
4.1Basic Data Binding With Interpolation05:35
4.2Property Bindings07:07
4.3Attribute Bindings03:29
4.4Event Bindings08:16
4.5Class and Style Bindings05:44
4.6The `NgClass` and `NgStyle` Directives05:04
4.7The `*ngIf` Directive04:41
4.8The `*ngFor` Directive09:29
4.9Inputs05:33
4.10Using Pipes in a Template07:31
4.11Using Pipes in a Class08:27
5.Forms10 lessons, 1:45:41
5.1Handling User Input With Template Reference Variables07:06
5.2Template-Driven Forms11:10
5.3Template-Driven Forms: Validation and Submission14:00
5.4Reactive Forms11:26
5.5Using a `FormBuilder`08:01
5.6Reactive Validation With Built-in Validators14:53
5.7Creating Custom Validators for Template-Driven Forms12:18
5.8Creating Custom Validators for Reactive Forms08:26
5.9Observing Form State Changes12:40
5.10Working With the `@HostListener` Decorator05:41
6.Routing9 lessons, 1:15:10
6.1Defining and Configuring Routes07:53
6.2Rendering Components With Router Outlets10:14
6.3Using Router Links for Navigation05:25
6.4Navigating Routes Using the Router06:24
6.5Determining the Active Route Using an Activated Route07:16
6.6Working With Route Parameters10:42
6.7Using Route Guards07:36
6.8Observing Router Events10:55
6.9Adding Child Routes08:45
7.Using the HTTP Client5 lessons, 56:24
7.1Sending an HTTP Request10:52
7.2Handling an HTTP Response11:22
7.3Setting Request Headers12:33
7.4Intercepting Requests09:04
7.5Finishing the Example Application12:33
8.Testing10 lessons, 1:23:27
8.1Service Unit Test Preparation10:45
8.2Unit Testing Services13:24
8.3Component Unit Test Preparation12:35
8.4Unit Testing Components07:27
8.5Unit Testing Component Templates06:58
8.6Unit Testing Pipes04:41
8.7Unit Testing Directives04:56
8.8Unit Testing Validators04:48
8.9Unit Testing Observables11:37
8.10Unit Testing HTTP Interceptors06:16
9.Building for Production1 lesson, 03:40
9.1Building for Production03:40
10.Conclusion1 lesson, 01:32
10.1Conclusion01:32
4.8 The `*ngFor` Directive
Hi folks. In this lesson, we're going to look at the ngFor structural directive. This directive is used to add repeated blocks of mark-up, based on some collection of items. So if we have a list of objects, we can display a chunk of mark-up for each item in the array. It's super useful and so very common. Our example application will have some lists of objects. Each player has a list of cards in their hand, so we can use ngFor to display the cards in the players' hands. Let's go to the game component. Inside the template there's a list element containing a single list item with the class card. We'll need one of these list items for each card in the player's hand. With the ngFor directive, we use the star syntax. The right-hand part of the directive is different than other template features in Angular. Unlike directives and bindings, the right-hand part is not a template expression. Instead, this is a special micro syntax that Angular interprets. We start by declaring a template variable using let, and then specifying the name of the variable, card in this case. We then use the keyword of, to specify the collection that we would like the loop to iterate. In this case, the hand property of the player. The ngFor directive can only be used with iterable properties, like arrays. It can't be used with objects. We also want to add some different class names, which we can do with some simple interpolation. We can add classes for the color of the card, the suit and either the name or value of the card. We can also update the span inside the list item. So that's the template handled, but we need to make some changes to the game components class as well. So first of all, we'll need to import the player model. We can also define the player property. So the player property will be of the type Player. We don't have anything in place yet to start a proper game. So let's just import the deck and create a player manually, so that we can see the ngFor working. So let's import the deck first of all. And now in the ngOnInit, let's create a new player and assign it to the player property. So one last thing we need to do, is have some extra styling for the cards. I've got a style sheet containing these styles on my desktop ready. So, I'm just gonna drag this into the SaaS folder. And let's just open that up and take a quick look. So the reason why I've already created this file and I'm just dragging it in is, because I'm sure nobody wants to sit there and watch me type out all of these styles. Cuz there really are a lot of them. So we've got the styles in place now. We should have a player, and that player should have some cards. So let's try and start the game and see what happens. So we can see that we don't see any cards at this point. Doesn't look like we've got any errors in the console. Let's just take a quick look through things, so the game component has a public property called player. I was just doing a cheeky console log and see that everything has been initialized properly. It does look like everything has been initialized correctly. Looks good to me. Okay, let's go back and check the template. And certainly looks like everything is in place. It looks like there is an arrow in the deck. Let's just see what's happening there. So it looks like I had an extra square bracket in there. Okay, so that should be fixed now. Let's go back and see if the browser is now working in the way that it should do. And the compiler seems happy now. And we still didn't see any cards. And the reason why we don't see any cards actually is because I've dragged the stylesheet in, but we just need to import that now into the main style sheet Let's try it again. And we have some cards. Excellent. So the ngFor will repeat a block of mock up for each item in an array. The array in this case, is the array of cards in the player's hand. And we know that there are two cards in the player's hand, so the block of mark up is been repeated twice. So let's also add the player's score now in the game component. We can do that with a simple interpolation binding. And let's go back to the browser again. Now we find that the score is displayed alongside the cards. We need to use an almost identical instance of the ngFor directive in the end component as well, so we might as we'll add that now. This time it's not gonna be the player property, it's gonna be the dealer property. And it's the same here, instead of letcardofplayer.hand, it's going to be letcardofdealer.hand. Aside from that it's identical, and we better add this dealer property in the end component class. So the dealer will just be a type of player. We haven't got the player model yet, so we'll need to bring that in as well. And now we're all set. So in this lesson, we've learned a little bit about the ngFor directive, and we saw that we can use it to create repeated blocks of mark-up, based on an iterable property of the component. We learned that we define a template variable which contains one of each from the iterable collection, and which we can use for the data of each repeated block of mark-up. Thanks for watching.