Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,950 --> 00:00:05,460
In this video we're going to take a look at how to build our own custom structural directive.
2
00:00:05,460 --> 00:00:09,060
Remember examples of structural directive are energy for and energy.
3
00:00:09,060 --> 00:00:15,030
If these are directives that change the structure of our HCM out let me first tell you about the purpose
4
00:00:15,120 --> 00:00:15,660
of the directive.
5
00:00:15,660 --> 00:00:18,830
We're going to create at the very bottom of this template file.
6
00:00:18,840 --> 00:00:21,000
I can give you a very quick example.
7
00:00:21,060 --> 00:00:25,570
I'm gonna write out a U L with an ally inside of it inside the ally.
8
00:00:25,580 --> 00:00:27,990
I'll say something like Hi there.
9
00:00:27,990 --> 00:00:32,400
I want to make a directive and we're gonna call it something like app times that's going to take in
10
00:00:32,460 --> 00:00:36,170
a number then whatever number we provide right there.
11
00:00:36,240 --> 00:00:41,090
This directive is going to take a look at the contents of the element of the contents of the UL.
12
00:00:41,100 --> 00:00:46,920
In this case it's the ally that's going to duplicate the contents five times over that we would end
13
00:00:46,920 --> 00:00:48,600
up with something like this.
14
00:00:48,720 --> 00:00:53,710
When this actually runs inside the browser it's pretty simple and straightforward structural directive.
15
00:00:53,730 --> 00:00:56,110
Let's take the contents duplicate it.
16
00:00:56,270 --> 00:01:00,570
It might sound like a very simple and kind of silly directive but believe it or not we're going to be
17
00:01:00,570 --> 00:01:05,610
able to actually replace the energy for directive up here with a custom one that you and I are about
18
00:01:05,610 --> 00:01:06,540
to make.
19
00:01:06,750 --> 00:01:09,870
It actually has pretty good and solid purpose.
20
00:01:09,870 --> 00:01:10,150
All right.
21
00:01:10,170 --> 00:01:12,780
So let's start to work on the structural directive debt.
22
00:01:12,780 --> 00:01:16,620
Number one is to generate a new directive at our terminal.
23
00:01:16,670 --> 00:01:21,900
We gonna flip on over to my terminal I'm inside of my pages directory and once again I'll do an energy
24
00:01:22,920 --> 00:01:28,800
generate directive and we're going to call this directive times because we're going to like duplicate
25
00:01:29,130 --> 00:01:36,480
some elements some number of times we'll run that and we've got a new file created called times not
26
00:01:36,480 --> 00:01:38,120
directive thought T.S..
27
00:01:38,550 --> 00:01:45,220
Let's flip back over to our Ed and find that file here's times not directive to us and you'll notice
28
00:01:45,220 --> 00:01:50,110
that we ended up with the exact same starter code that we had for the other directive though our other
29
00:01:50,110 --> 00:01:54,160
directive we put together of class directive was an attribute directive.
30
00:01:54,160 --> 00:01:56,610
This one right here is a structural directive.
31
00:01:56,740 --> 00:02:01,330
It turns out that we first generate these files the code inside them pulley identical it's what code
32
00:02:01,330 --> 00:02:05,620
we add inside of here that determines whether or not this is going to be structural in nature or some
33
00:02:05,710 --> 00:02:11,050
attribute changing thing in nature and the first big difference between these two types of directives
34
00:02:11,290 --> 00:02:16,090
is gonna be to the different things we import at the top with our structural directive over here.
35
00:02:16,090 --> 00:02:21,060
We're going to import to kind of nasty sounding things don't worry it's not going to be that bad.
36
00:02:21,280 --> 00:02:25,450
We're gonna add in three imports in total two of them are gonna be kind of specific for our structural
37
00:02:25,450 --> 00:02:26,560
directive.
38
00:02:26,740 --> 00:02:34,900
We're going to get template ref u container F and then input input right here.
39
00:02:34,920 --> 00:02:39,510
Same kind of input decorator that we've seen a couple of times but template wrap and view container
40
00:02:39,510 --> 00:02:45,270
F are definitely new things we're going to use these to customize the arguments that are accepted into
41
00:02:45,270 --> 00:02:50,670
our constructor let's just write out some code all very quickly show you what is going on inside of
42
00:02:50,670 --> 00:02:54,860
my constructor list I going to first put a new line in just to give myself a little bit of space is
43
00:02:54,900 --> 00:03:01,350
we're gonna write out some kind of long arguments here gonna first say private you container and that's
44
00:03:01,350 --> 00:03:09,560
going to be a type you container wrap then as the second argument I'm gonna get private template ref
45
00:03:10,380 --> 00:03:16,010
and that's going to be a template ref that is a generic type inside of here I'm going to put in any
46
00:03:16,490 --> 00:03:18,920
and I just noticed I misspelled private so many pick setup
47
00:03:22,210 --> 00:03:23,620
but what are these.
48
00:03:23,620 --> 00:03:29,930
Well the view container is a reference to the element that we applied our directive to.
49
00:03:30,260 --> 00:03:35,470
So in other words if we think back to the UL example I just showed you like this right here we are applying
50
00:03:35,470 --> 00:03:41,810
at times to the U L element though in this case view container would be like a reference to that you
51
00:03:41,870 --> 00:03:46,930
well you might hear that and merely say wait a minute isn't that just what we got back over inside the
52
00:03:46,930 --> 00:03:48,950
class directive with element ref.
53
00:03:48,950 --> 00:03:55,300
Well it's a little bit different this view container ref is a kind of custom version of our element
54
00:03:55,560 --> 00:04:00,910
it gives us the ability to very easily add in more elements or remove elements or essentially render
55
00:04:00,910 --> 00:04:02,950
some other templates inside there.
56
00:04:03,030 --> 00:04:07,870
So even though it is kind of a reference to this wrapping element it's a version of it that makes it
57
00:04:07,870 --> 00:04:10,080
really easy to work with the contents of it.
58
00:04:10,270 --> 00:04:17,280
Hence the name view container wrap because it's containing some other elements than the other property
59
00:04:17,280 --> 00:04:24,420
or the other argument of template ref is a reference to whatever elements are placed inside of the element
60
00:04:24,450 --> 00:04:28,790
that we applied our directive to though in this case it would be a reference more or less to like the
61
00:04:28,810 --> 00:04:36,590
ally and anything else we does we actually write out inside of here that these first two arguments now
62
00:04:36,650 --> 00:04:40,670
with our constructor we're not going to write anything else or any other initialization inside there
63
00:04:40,930 --> 00:04:48,080
that's pretty much good as is the next and we're going to add in is another one of those setter methods.
64
00:04:48,350 --> 00:04:52,710
So as we discussed just a moment to go we want to eventually have something that looks like this right
65
00:04:52,710 --> 00:04:58,140
here something it says at times equals five but this ends up being some very similar syntax to what
66
00:04:58,140 --> 00:05:01,430
we just discussed around our class directive up here.
67
00:05:01,590 --> 00:05:06,690
Remember we had at class this one little statement right here both specified that we wanted to use that
68
00:05:06,690 --> 00:05:13,410
directive and that the app class property on our directive class as well we're going to do the same
69
00:05:13,410 --> 00:05:17,350
thing this time around with a different name of app times.
70
00:05:17,370 --> 00:05:24,250
So in other words echo over here I'm going to add in an input decorator I'm going to use that kind of
71
00:05:24,340 --> 00:05:27,260
input aliasing feature that we've just discussed.
72
00:05:27,430 --> 00:05:33,880
I say whenever the at times property is set on an element we're going to instead that a property called
73
00:05:35,280 --> 00:05:43,080
render and inside of here we're going to receive the number of times that we're supposed to render out
74
00:05:43,080 --> 00:05:47,490
the containing elements as an argument okay.
75
00:05:47,580 --> 00:05:51,700
Now the code in here as you're kind of sensing you're probably getting a sense that these like structural
76
00:05:51,700 --> 00:05:54,060
directive things are a little bit more complicated.
77
00:05:54,070 --> 00:05:58,350
Yeah they kind of are as we start to use these view container and template wrap things you're gonna
78
00:05:58,370 --> 00:06:03,460
see the stuff is a little bit more nasty and the very simple attribute directives or at least relatively
79
00:06:03,460 --> 00:06:08,200
simple we're going to write out just a couple of lines of code that are really going to implement our
80
00:06:08,200 --> 00:06:09,510
entire directive.
81
00:06:09,580 --> 00:06:11,220
They're going to be a little bit mysterious.
82
00:06:11,320 --> 00:06:19,280
The first thing to say is this dot view container not clear then after that I'm going to iterate from
83
00:06:19,280 --> 00:06:26,330
zero all the way up to times and for every step of iteration I'm going to create a new instance of this
84
00:06:26,330 --> 00:06:30,830
template ref and we're going to add it into the view container.
85
00:06:30,830 --> 00:06:40,450
I'll say for that I equals zero I less than times I plus plus and then inside I'll do this dot view
86
00:06:40,630 --> 00:06:50,510
container not create embedded view as the first argument I'll pass in this dot template ref as a second
87
00:06:50,540 --> 00:06:54,940
argument I can pass on an empty object for right now OK.
88
00:06:54,970 --> 00:06:56,030
So kind of nasty stuff here.
89
00:06:56,060 --> 00:06:57,570
So what's really going on.
90
00:06:57,590 --> 00:07:03,290
Well remember we're going to be potentially setting the at times property several times though whenever
91
00:07:03,290 --> 00:07:08,330
we write out the five right here this is putting in a fixed value of five but we could potentially provide
92
00:07:08,360 --> 00:07:12,280
a reference to some method on our app components like it.
93
00:07:12,440 --> 00:07:17,700
Number of times and the value returned from this might be changing over time.
94
00:07:17,800 --> 00:07:21,770
So if that value changes over time that means that we're going to want to show different amounts of
95
00:07:21,770 --> 00:07:23,640
allies over time as well.
96
00:07:23,780 --> 00:07:29,720
The first thing we do inside of this render method is we clear out any elements that are currently inside
97
00:07:29,750 --> 00:07:30,900
of our view container.
98
00:07:31,020 --> 00:07:34,260
And remember the view container is like this you all thing right here.
99
00:07:34,450 --> 00:07:39,070
But the number of times we're going to render this changes we're going to delete everything inside it
100
00:07:39,080 --> 00:07:43,950
is already existing and we're going to recreate everything inside there from scratch.
101
00:07:44,010 --> 00:07:49,100
This line of code right here is going to take a look at our view container and it's going to try and
102
00:07:49,100 --> 00:07:55,130
create some new HDL inside that's what this create embedded view is the HCM all that we're going to
103
00:07:55,130 --> 00:07:57,810
create is provided by the template ref.
104
00:07:57,920 --> 00:08:03,200
And again the template ref is more or less the H team L that we had placed inside of the element we
105
00:08:03,200 --> 00:08:06,090
applied our directive to as I just mentioned.
106
00:08:06,150 --> 00:08:06,410
Yeah.
107
00:08:06,420 --> 00:08:08,600
This stuff is a little bit more complicated.
108
00:08:08,700 --> 00:08:12,900
I really just want to give you kind of a very practical example of how to create a structural directive
109
00:08:13,310 --> 00:08:19,040
but yeah to be honest I don't think you're going to be doing this very frequently.
110
00:08:19,080 --> 00:08:22,200
So believe it or not we pretty much have enough to actually test out here.
111
00:08:22,320 --> 00:08:29,100
Going to replace that get number of times with just the number five I'm going to save this I'll flip
112
00:08:29,100 --> 00:08:32,640
back over to my application inside the browser and at the bottom you'll see.
113
00:08:32,640 --> 00:08:37,830
Sure enough we've got high there repeated five times to even if it is mysterious on how this stuff is
114
00:08:37,830 --> 00:08:40,620
working it does actually work.
115
00:08:40,840 --> 00:08:45,100
Now we've got this directive together let's delete this will you all down here.
116
00:08:45,100 --> 00:08:49,810
And when we come back the next video I'm gonna show you how we can make another little change that directive
117
00:08:50,110 --> 00:08:53,620
and then use it to replace the existing energy for that we have right here.
13598
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.