- Overview
- Transcript
4.10 Using Pipes in a Template
Pipes are a useful way to filter the data that gets displayed in a template. In this lesson we'll see how to create and make use of a pipe.
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.10 Using Pipes in a Template
Hi folks. In this lesson we're going to look at pipes. If you use the original AngularJS, pipes are just an updated version with filters. If you didn't use any previous version of Angular, a pipe is a way to filter the data that is driving the view, like if you're using an ng4 to create a list of products, a pipe can filter that list and only show items if they meet some criteria. They're called pipes because of the syntax that we use in the template as you'll see shortly. So pipes are a bit like mini components. They have a decorator but they don't have a template or any styling. They're basically just a little unit of functionality that can filter data. We can use the CLI to create one for us. Let's say we want to be able to filter the cards in a player's hand. Let's create a face pipe which can filter out any face cards like kings or queens out of the player's hand. So the CLI will create the pipe class file for us, and it will create a spec file for us, and it will also update the module for us. And the CLI has done the thing where it names the folder incorrectly. Let's just fix that briefly. And here we have the basis for our pipe. So when the CLI added the new pipe to the module for us, it would have done that using the wrong name. So we just need to fix that as well. And you can see that the face cards pipe has been added as a declaration with the rest of the components. So a pipe really is just like a mini component with no templates and no styling. So let's just take a quick look at what we get in the skeleton of the pipe. We get a couple of things imported for us, the pipe decorator and the pipe transform interface, which the pipe class implements. To satisfy this interface, the class must provide a transform method, which has also been added for us. When the pipe is in use, Angular will call this method of the pipe and pass it the data to filter as the first argument. And then any additional arguments, as the second arguments. The pipe is pretty flexible by default, each parameter is of the any type, and it also returns the any type. So we can basically do whatever we want in here. What else happens? Well, the decorator for this type of thing in Angular is pretty straightforward. We just provide the name of the pipe, which is what we'll refer to from our template. So, let's just make some changes. We're going to use this pipe in the template for the game component where we display the player's hand. So the data that the pipe will be passed will be this array of cards. So the first argument has been renamed cards and it will be an array of card objects. And you can see straight away that this file doesn't know what a card object is so we better import our card model. We will also want our method to return an array of cards. So now inside the transform method, we're going to want to iterate the array of cards and check each card individually. If the card is a face card, it will have a value of either 10 or if aces are high perhaps 11. We'll want to filter the array so that it contains only cards with a value of less than ten. Great, that ought to do it. We use the filter method, the arrow function will be invoked for each item in the array. And will be passed the item, which will be a card, from the current invocation. We can return true if the value is less than 10, or false otherwise. So now we can go to the game component and implement the pipe. So we do this in the expression for the ngFor directive and we can just add it to the end. We use the pipe symbol at the end of the existing expression and this is why pipes are called pipes. And then we specify the name of the pipe, which in this case is faceCards. Any additional arguments can then be passed to the pipe using a colon and then the name of the item that we'd like to parse in as an argument. This time we're going to be parsing in easy mode. And that will either be true or false, depending on whether the player has chosen to start the game in easy mode or not. So we don't have this property easy mode yet. Let's go to the game component and add that. And we can just hard code that to true for now. And let's go back to the pipe and just update the argument there also. So if easy mode is false, then we don't want to filter any cards, so we can just return the existing array of cards So, let's make sure all the files are saved, and let's go back to the browser and take a look. So we should find that we have a slight issue. If the player is dealt any face cards, the filter will remove them, but it won't replace them with non-face cards. So the player could have only one card, or no cards at all, such as we can see here. The problem is that we're applying the filter too late. In this particular case, the template is the wrong place for the pipe, although that's not usually the case. It's just the specific requirements of the example application. Luckily for us though, we can also use pipes in pure code as well as in templates. So we'll revisit this pipe in another lesson. For now, let's take the pipe back out of the component. So in this lesson we looked at pipes, and we saw that they are useful classes that we can use to filter the data that gets displayed in a template. We used the custom pipe in this lesson in conjunction with an NgFor directive, but we don't have to use them with NgFor. We can use them with other parts of our templates as well. As well as custom pipes, Angular also has a number of built in pipes for filtering or otherwise formatting data, such as the decimal pipe, the currency pipe, and the date pipe. Thanks for watching.