Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,880 --> 00:00:03,900
In this video we're going to take a look at something really interesting and fun.
2
00:00:03,910 --> 00:00:07,150
We're going to try to create our own custom directive.
3
00:00:07,330 --> 00:00:09,000
Let me tell you about what we're gonna build.
4
00:00:09,010 --> 00:00:14,020
But first I'm going to delete all this energy switch stuff at the very bottom and uncommon at the div
5
00:00:14,050 --> 00:00:15,910
with the H or in image inside of it.
6
00:00:16,910 --> 00:00:17,200
OK.
7
00:00:17,230 --> 00:00:19,610
So what are we going to make with a custom directive.
8
00:00:19,610 --> 00:00:22,840
Well it can be really simple and straightforward to understand what we're going to make.
9
00:00:22,850 --> 00:00:28,730
We're going to rebuild a portion of the energy class directive from scratch.
10
00:00:28,730 --> 00:00:29,800
That's gonna be a lot of fun.
11
00:00:29,840 --> 00:00:32,600
We're gonna see how this thing kind of works behind the scenes.
12
00:00:32,600 --> 00:00:34,680
Now I said that we're going to rebuild a portion of it.
13
00:00:34,700 --> 00:00:40,490
So specifically we're gonna make our own directive that accepts this kind of argument like an object
14
00:00:40,820 --> 00:00:43,740
where we're going to have some keys and a boolean values.
15
00:00:43,900 --> 00:00:48,710
And if that boolean value is true we're going to apply that key as a class to the element.
16
00:00:48,710 --> 00:00:54,800
Otherwise if the billion value is false we will remove that class from the element at the very end of
17
00:00:54,800 --> 00:00:55,210
the day.
18
00:00:55,220 --> 00:01:00,080
We're going to essentially be able to go through this file and replace the energy class elements or
19
00:01:00,080 --> 00:01:04,820
the engine class directives excuse me that you see inside of here with our own custom ones like custom
20
00:01:05,150 --> 00:01:07,670
directive or whatever we end up calling Rs.
21
00:01:07,670 --> 00:01:12,840
That's the goal but the first thing we have to do is generate a new directive.
22
00:01:12,840 --> 00:01:18,660
We're gonna generate a directive using the angular the ally as we've used to generate pipes and components
23
00:01:18,690 --> 00:01:19,650
already.
24
00:01:19,680 --> 00:01:27,210
I'm going to flip back over to my terminal and inside my pages directory I will run and G generate directive.
25
00:01:27,370 --> 00:01:30,030
We're going to call our directive simply class
26
00:01:33,200 --> 00:01:33,530
all right.
27
00:01:33,560 --> 00:01:39,050
So it has created a new file inside of our app directory world class not directive not to.
28
00:01:39,370 --> 00:01:43,500
So let's go check that out inside my app directory.
29
00:01:43,520 --> 00:01:45,210
I'll find a class dot directive dot.
30
00:01:45,250 --> 00:01:50,090
Yes but this is all the code we get when we first make a directive that's it.
31
00:01:50,090 --> 00:01:52,550
We don't get anything else whatsoever.
32
00:01:52,670 --> 00:01:57,620
So unlike the pipe that we generated a little bit ago remember when we generate a pipe we get that transform
33
00:01:57,620 --> 00:02:02,540
method and it's pretty easy to get started writing code right away with a directive.
34
00:02:02,540 --> 00:02:07,670
Now we don't really get a lot of help the first two we need to understand is just how we make use of
35
00:02:07,670 --> 00:02:08,970
this directive.
36
00:02:09,020 --> 00:02:14,300
Well if we look at the decorator right here you'll see that has a selector property but the angle brackets
37
00:02:14,300 --> 00:02:17,270
right here mean that you make use of this directive.
38
00:02:17,270 --> 00:02:23,950
We're going to add the word app class as an attribute on some element inside of a template.
39
00:02:24,140 --> 00:02:30,770
For example if I go back over to my app component right now and on this very first ally I'm going to
40
00:02:30,770 --> 00:02:38,620
add in just the word app class like so that's how we would make use of our directive right now.
41
00:02:38,640 --> 00:02:43,080
Now just to make sure that it's actually being used inside my constructor I'll add in a console log
42
00:02:43,290 --> 00:02:46,660
of last directive used.
43
00:02:46,740 --> 00:02:52,680
Like so let's then save this and then flip back over to our browser once I'm back over here I can open
44
00:02:52,680 --> 00:02:54,580
up my console and I'll see right away.
45
00:02:54,630 --> 00:02:59,730
Yep there is a class directive used that is just how we can generate a directive and then immediately
46
00:02:59,760 --> 00:03:02,200
apply it to some element.
47
00:03:02,210 --> 00:03:02,580
Yes.
48
00:03:02,610 --> 00:03:05,310
Now we've got this thing generated and we understand we're trying to build.
49
00:03:05,310 --> 00:03:09,060
Let's take a quick pause right here and then start to play around with this directive in the next video.
5155
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.