Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,970 --> 00:00:04,080
In this video we're going to figure out how to generate our own custom pipe.
2
00:00:04,120 --> 00:00:10,120
The goal of our custom pipe is to take in some distance in miles and then convert it into kilometers.
3
00:00:10,150 --> 00:00:14,330
We're going to first get started by building it another little input output pair.
4
00:00:14,350 --> 00:00:17,140
We're going to add another property to our component class.
5
00:00:17,140 --> 00:00:18,880
And then after that it will generate a new pipe.
6
00:00:19,390 --> 00:00:25,270
Let's get to it first get started by opening up my app component once again inside of here.
7
00:00:25,270 --> 00:00:29,460
I'm going to declare a new property of Miles.
8
00:00:29,540 --> 00:00:31,550
Well then add on an event handler for this as well.
9
00:00:31,580 --> 00:00:32,230
So on.
10
00:00:32,270 --> 00:00:39,320
Miles change I'll say value is going to be a string was signed that two miles.
11
00:00:39,340 --> 00:00:46,320
And once again going to do a pass float on it Next up will open up our component template.
12
00:00:46,340 --> 00:00:47,380
It is right here.
13
00:00:47,620 --> 00:00:53,170
And just as we've done several times I got to find one of these div of class roadblocks right here to
14
00:00:53,250 --> 00:00:56,830
highlight the thing and I'll duplicate it right underneath itself.
15
00:00:57,080 --> 00:01:02,640
So and then going to zoom back in and we'll change a couple of properties on here.
16
00:01:02,690 --> 00:01:12,590
First off I'll change the label two miles will change the callback right here too on Miles change I'll
17
00:01:12,590 --> 00:01:20,390
change the output label down here two kilometers and then right now I'll just print out Miles like so
18
00:01:21,410 --> 00:01:21,690
okay.
19
00:01:21,720 --> 00:01:24,700
So let's say this look back over see how we're doing.
20
00:01:24,750 --> 00:01:29,330
Well I can see the input right there and if I start to type in some number Yep I see it over here.
21
00:01:29,400 --> 00:01:30,330
Very good.
22
00:01:30,330 --> 00:01:35,570
Now it's time for us to generate a pipe we generate pipes in the same way that we generate components.
23
00:01:35,580 --> 00:01:38,850
So in other words we make use of the angular CLIA.
24
00:01:38,970 --> 00:01:41,810
Let's change on over to our command line and run the command.
25
00:01:41,820 --> 00:01:47,330
That will generate a new pipe for us inside of my project director right here.
26
00:01:47,440 --> 00:01:48,590
It's on my project directory.
27
00:01:48,590 --> 00:01:56,010
I will run energy generate hype and in the name of our pipe I'm going gonna color pipe simply convert.
28
00:01:56,180 --> 00:01:59,050
We are converting from miles to kilometers.
29
00:01:59,090 --> 00:02:03,100
There might be a better name for this than convert since we are doing a very specific conversion.
30
00:02:03,200 --> 00:02:08,810
But this will work for right now I'll run that command and we'll see that two files are generated and
31
00:02:08,810 --> 00:02:15,650
one file is updated the file that we really care about here is the convert that pipe dot t s file.
32
00:02:15,650 --> 00:02:22,790
Let's go open up that file and see what we got back inside my Ed. I'll find the app directory and inside
33
00:02:22,790 --> 00:02:25,940
of there is the convert dot up dot T.S. file.
34
00:02:26,110 --> 00:02:32,290
This is a pipe right here the whole pipe is to be honest the kind of similar or simple in nature.
35
00:02:32,440 --> 00:02:36,620
It really just has one important function side of it called transform.
36
00:02:36,640 --> 00:02:38,390
Meantime we make use of a pipe.
37
00:02:38,470 --> 00:02:41,220
This transform function is going to be invoked.
38
00:02:41,350 --> 00:02:44,770
We're going to receive some input value as the first arguments.
39
00:02:44,800 --> 00:02:49,940
And it's our job to do some calculation inside of here and then return some new value.
40
00:02:49,960 --> 00:02:54,820
Let's take a look at how we use this in a very basic and simple form going to first go back over to
41
00:02:54,820 --> 00:03:00,370
my component template a find where we are currently printing out Miles and I'll pipe it through the
42
00:03:00,430 --> 00:03:06,120
convert pipe that we just generated you'll notice that as soon as I add that on I get a little air here
43
00:03:06,130 --> 00:03:10,070
and if I mouse over that it says might convert could not be found.
44
00:03:10,220 --> 00:03:14,910
If you see this area as well this is really just an air coming from your editor.
45
00:03:15,190 --> 00:03:23,260
You can fix it by going up to view binding command pallet and in search for reload window if you do
46
00:03:23,260 --> 00:03:27,950
that that's going to essentially re initialize TypeScript and your editor the typescript checker.
47
00:03:28,030 --> 00:03:35,010
And now that air will go away I can now mouse over convert and it correctly says that this is a pipe.
48
00:03:35,020 --> 00:03:38,470
The interesting thing about making use of these pipes that we generate is that we don't really have
49
00:03:38,470 --> 00:03:41,750
to add in any kind of import statement or anything like that.
50
00:03:41,800 --> 00:03:45,950
We just go ahead and make use the pipe and we're good to go okay.
51
00:03:45,960 --> 00:03:51,470
Now that we are hyping that value into our convert pipe inside my transformer method right here.
52
00:03:51,580 --> 00:03:56,070
Gonna do a console log of the value that flows in I'll then save this.
53
00:03:56,160 --> 00:04:01,380
I'm gonna flip back over and let's see what our console log is right away.
54
00:04:01,380 --> 00:04:06,840
I'll see it I get a console log of undefined as because inside of our app component the miles property
55
00:04:06,900 --> 00:04:08,190
starts off is undefined.
56
00:04:08,190 --> 00:04:10,400
We never actually set a value to it.
57
00:04:10,440 --> 00:04:13,930
We only declared that property inside of app component.
58
00:04:14,000 --> 00:04:14,400
Yes.
59
00:04:14,400 --> 00:04:18,150
Right here we have Miles when this complaint is first created.
60
00:04:18,150 --> 00:04:22,860
There is no value for the Miles property yet it's not until we actually type inside of our input that
61
00:04:22,860 --> 00:04:27,320
this property is going to be updated as soon as we start to type inside that input.
62
00:04:27,330 --> 00:04:34,420
Now a value is being set and we can see one two three or one than one two and one two three appear we
63
00:04:34,420 --> 00:04:39,130
no longer see an output right here on the screen because our transform function is not actually returning
64
00:04:39,130 --> 00:04:40,150
anything.
65
00:04:40,150 --> 00:04:41,680
Let's try changing that right now.
66
00:04:43,080 --> 00:04:49,960
Back inside my pipe I'm going to take whatever value we get and I'm going to add 20 to it we'll save
67
00:04:49,960 --> 00:04:52,390
this look back over and you'll notice.
68
00:04:52,380 --> 00:04:57,730
Now we start off with not a number that's because we took the value undefined and tried to add a number
69
00:04:57,730 --> 00:04:58,520
to it.
70
00:04:58,570 --> 00:05:00,810
Result of that operation is not a number.
71
00:05:00,940 --> 00:05:04,450
We'll probably want to figure out some way to handle that case is obviously we probably don't want to
72
00:05:04,450 --> 00:05:09,540
show not a number at any point time to a user by now entering one.
73
00:05:09,670 --> 00:05:14,500
I see that I get the results of one plus 20 okay.
74
00:05:14,580 --> 00:05:17,870
That's the basics of pipe really just taken a value.
75
00:05:17,870 --> 00:05:20,270
We modify the value and return it.
76
00:05:20,270 --> 00:05:24,470
Well let's now try to handle the case in which we receive a value that is undefined.
77
00:05:24,500 --> 00:05:26,410
So that's when we were just showing not a number.
78
00:05:26,450 --> 00:05:28,310
We definitely want to show not a number.
79
00:05:28,340 --> 00:05:32,390
We also take care of the case in which value is defined and we actually have a number and we want to
80
00:05:32,390 --> 00:05:34,710
convert it over to kilometers.
81
00:05:35,060 --> 00:05:37,220
But I handled a case in which value is not defined.
82
00:05:37,220 --> 00:05:43,790
We can add in a very simple if statement and say if there is no value that is passed in and just return
83
00:05:43,910 --> 00:05:47,890
empty string.
84
00:05:47,900 --> 00:05:54,440
Now if I flip back over I don't see anything until I start to type in an actual value.
85
00:05:54,510 --> 00:05:55,810
Well that was simple enough.
86
00:05:55,830 --> 00:06:01,270
Now we can add in some math to do a conversion from our value in miles over two kilometers.
87
00:06:01,500 --> 00:06:05,220
Really quickly let's look up that conversion constant inside of Google.
88
00:06:05,220 --> 00:06:12,650
New tab right here I'm going to do a quick search and I'll say Miles you can to get from one mile over
89
00:06:12,650 --> 00:06:16,950
two kilometers we multiply by one point six O nine.
90
00:06:17,150 --> 00:06:23,320
We're going to take that value I'll take my value right here I'm going to multiply it by that value
91
00:06:25,810 --> 00:06:32,370
OK so I'll save this flip back over and now if I enter in one mile I get the equivalent distance in
92
00:06:32,370 --> 00:06:37,990
kilometers or one hundred one thousand two hundred whatever else.
93
00:06:38,060 --> 00:06:43,010
Well that's the basics of creating our own pipes but as you can see they're pretty easy to generate
94
00:06:43,100 --> 00:06:44,140
very easy to write.
95
00:06:44,180 --> 00:06:46,090
Very easy to make use of.
96
00:06:46,370 --> 00:06:49,780
We're going to use pipes every now and then inside of our application.
97
00:06:49,790 --> 00:06:54,980
Any time we feel like we need to do some kind of like custom transformation now we've seen this quick
98
00:06:54,980 --> 00:06:57,020
pause right here move on in the next video.
10510
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.