Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,160 --> 00:00:04,730
In this video I want to take a look at one or two little gotchas around pipes.
2
00:00:04,730 --> 00:00:07,070
Just some things that you need to be aware of.
3
00:00:07,100 --> 00:00:11,240
The first thing you need to be aware of is that although we've been using pipes quite a bit with this
4
00:00:11,240 --> 00:00:16,430
interpolation syntax you can technically use pipes almost anywhere inside of a template.
5
00:00:16,430 --> 00:00:18,060
Let me give you a quick example.
6
00:00:18,290 --> 00:00:24,410
Let's add in a new requirement to our project and let's say that if we have some enter entry for miles
7
00:00:24,640 --> 00:00:28,010
that is less than 10 maybe we don't want to do a conversion at all.
8
00:00:28,580 --> 00:00:32,660
Let's say that if we enter something less than 10 miles I don't want to display this.
9
00:00:33,390 --> 00:00:38,270
But any time that you want to toggle the visibility of some element we can use that energy if structural
10
00:00:38,270 --> 00:00:39,430
directive.
11
00:00:39,580 --> 00:00:43,920
We could say energy if and then inside of here we could do our comparison very easily.
12
00:00:44,000 --> 00:00:47,980
So we could say miles less than an excuse me greater than 10.
13
00:00:47,990 --> 00:00:50,750
I said let's see it only if it's greater than 10.
14
00:00:50,980 --> 00:00:52,000
It's let's save this.
15
00:00:52,010 --> 00:00:58,280
Now we can flip back over and if I enter in miles of say for nothing shows up but as soon as I put in
16
00:00:58,340 --> 00:00:59,170
eleven.
17
00:00:59,180 --> 00:01:01,210
Now I get that display.
18
00:01:01,240 --> 00:01:01,460
OK.
19
00:01:01,520 --> 00:01:04,250
Well simple enough but how does that relate to pipes.
20
00:01:04,250 --> 00:01:06,200
Let's now convert change this just a little bit.
21
00:01:06,200 --> 00:01:11,600
Let's say that rather than displaying this output if Miles is greater than 10 maybe we only want to
22
00:01:11,600 --> 00:01:12,830
display this output.
23
00:01:12,830 --> 00:01:19,400
If we have some entry that is greater than 10 kilometers it would require us to somehow take the miles
24
00:01:19,730 --> 00:01:25,930
convert it into kilometers and then do the conversion rather than writing in some logic like directly
25
00:01:25,930 --> 00:01:29,410
here to say hey let's take Miles and multiply it by one point.
26
00:01:29,500 --> 00:01:34,380
So 9 4 or whatever it was we could instead just reuse the pipe that we already put together.
27
00:01:34,590 --> 00:01:38,640
But to do so you're going to be tempted to shoot first the wrong way to do this.
28
00:01:38,650 --> 00:01:42,920
You're going to be tempted to write up something like this convert two kilometers.
29
00:01:43,060 --> 00:01:44,530
That's not quite right.
30
00:01:44,530 --> 00:01:49,780
Instead whenever we use a pipe inside of some template syntax like we have right here we have to wrap
31
00:01:49,780 --> 00:01:52,150
it up in a set of parentheses.
32
00:01:52,320 --> 00:01:55,990
I can wrap that the parentheses and now the air seems to go away.
33
00:01:56,050 --> 00:02:01,840
Now we would read this as saying take a look at Miles inverted into kilometers and then make sure that
34
00:02:01,840 --> 00:02:03,260
that is greater than 10.
35
00:02:03,460 --> 00:02:11,080
If that is not greater than 10 then hide the entire div it's going to say this now look back over now
36
00:02:11,080 --> 00:02:17,290
if I put in just one mile I do not see anything but if I put in say like eight eight converts into a
37
00:02:17,290 --> 00:02:21,700
number of kilometers greater than 10 which means we're going to show that div in everything inside of
38
00:02:21,700 --> 00:02:22,310
it.
39
00:02:22,450 --> 00:02:26,380
Right now I see an output right here of like this one point two eight million or twelve million whatever
40
00:02:26,380 --> 00:02:27,480
it is.
41
00:02:27,570 --> 00:02:31,020
And that's because I still have a conversion down here of centimeters.
42
00:02:31,150 --> 00:02:35,570
If I change that to Cam example of make just a little bit more sense.
43
00:02:35,710 --> 00:02:37,320
Now if I put in say seven.
44
00:02:37,360 --> 00:02:37,930
There we go.
45
00:02:37,930 --> 00:02:42,040
It's clearly greater than ten because I've got eleven point two kilometers.
46
00:02:42,040 --> 00:02:46,810
Again the only thing you need to understand here is that we can use pipes in other locations than just
47
00:02:46,900 --> 00:02:49,300
inside of our interpolation syntax.
48
00:02:49,300 --> 00:02:50,870
You can use them inside of directives.
49
00:02:50,870 --> 00:02:56,850
We can use them inside of binding like property binding or event binding just about everywhere.
50
00:02:56,850 --> 00:03:00,910
And one thing you need to remember is that as soon as you start to use some other kind of expression
51
00:03:00,970 --> 00:03:06,370
like say a greater than sine or an assignment or anything like that just remember chances are you might
52
00:03:06,370 --> 00:03:09,870
need to wrap it with a set of parentheses OK.
53
00:03:09,920 --> 00:03:14,000
One other small thing that I want to discuss we're just going doing this video because it is a very
54
00:03:14,000 --> 00:03:19,990
very quick thing going to remove that energy if directive is going to change our conversion back over
55
00:03:19,990 --> 00:03:26,800
two centimeters if I save this pull back over and if I just enter like one mile right here.
56
00:03:26,800 --> 00:03:29,710
How about even like point 0 0 1 miles.
57
00:03:29,710 --> 00:03:33,790
You'll notice that as soon as I do I get a floating point math error right there.
58
00:03:33,790 --> 00:03:35,280
Obviously this is not quite right.
59
00:03:35,290 --> 00:03:38,680
It's just because of how javascript treats math in general.
60
00:03:38,680 --> 00:03:43,600
Let's imagine that a user enters point 0 0 1 and they see an output like that and they'd probably say
61
00:03:44,260 --> 00:03:47,950
what that doesn't look right there's something wrong with this application.
62
00:03:48,110 --> 00:03:53,050
A very easy way that we could fix this up would be to use that decimal pipe that we took a look at just
63
00:03:53,050 --> 00:03:54,380
a little bit ago.
64
00:03:54,490 --> 00:04:00,500
Remember we used the decimal pipe right up here and we used it by writing out the word number running
65
00:04:00,510 --> 00:04:07,080
out number refers to the decimal pipe so it would be great if we could do our conversion into centimeters
66
00:04:07,440 --> 00:04:11,160
and then take the output of that and then throw it into the decimal pipe.
67
00:04:11,160 --> 00:04:15,480
Make sure that we limit the number of decimal places that we show Inside the output.
68
00:04:15,830 --> 00:04:18,570
But to do so we essentially want to take Miles.
69
00:04:18,750 --> 00:04:21,940
Throw it into converts and then throw it into number.
70
00:04:21,940 --> 00:04:29,640
But all we have to do is write out another type symbol like so now we could put in another pipe over
71
00:04:29,640 --> 00:04:33,930
the number pipe right here we would probably want to customize it and say give us at least one digit
72
00:04:33,930 --> 00:04:36,030
to the left of the decimal.
73
00:04:36,030 --> 00:04:43,720
And then at most or at least zero excuse me at least zero to the right and at most maybe you like to.
74
00:04:43,810 --> 00:04:49,780
Now if I save that and I put in point 0 0 1 miles again now I see it kind of chopped off.
75
00:04:50,110 --> 00:04:57,220
And if I put in say 1 yep looks great So again we can use multiple different pipes together by just
76
00:04:57,220 --> 00:05:00,490
changing them with this kind of syntax like you see right here.
77
00:05:00,490 --> 00:05:00,720
All right.
78
00:05:00,730 --> 00:05:05,740
So that's two little interesting tidbits around pipes and that's pretty much all we're going to discuss
79
00:05:05,740 --> 00:05:07,080
around pipes right now.
80
00:05:07,120 --> 00:05:09,690
Let's take a pause right here and continue in just a moment.
8479
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.