7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial
FREELessons: 67Length: 8.9 hours

• 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.

## 10.Conclusion1 lesson, 01: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.

Back to the top