- Overview
- Transcript
2.3 The Application Component
In this lesson we'll see how the root component works with the module to define the root of our app. We will also customize this file to suit the example application.
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
2.3 The Application Component
Hi folks, in this lesson, we're going to take a look at the root component for our application, which the Angular CLI created for us. We can start out by taking a look at what we get from the CLI, and then we can modify this to suit our needs. One of the big differences between Angular 1.X, and Angular 2+, is that in Angular 2 and above, the component is the main building block of applications. A component is a single unit of our application, and is usually composed of a piece of view in the form of an HTML template. Some styling information created using Sass or regular CSS, and some TypeScript logic that defines the component's behavior. So let's open up the app.component.ts file. If you remember from the last lesson, the app component is the root component which gets bootstraps by the application module. Just like the app module which use the NG module decorator, components in Angular are also defined using a decorator. This is the component decorator and in fact, is the only identifier imported by the app component. This decorator also takes a configuration object, which specifies meta information about the component. We tell Angular the name of the custom element that Angular should render the component into, in this case, that is called app-root. If we open up the index.html file, We can see that this app-root custom element has been added into the body. So that's where the app.component will get rendered into. Another core part of the component is the HTML template. We tell Angular where the template is using the template URL property and in this case it maps to a file called app.component.html. And this contains the template for the app component. So this was generated for us by the Angular CLI and just contains some example mark up, which the comment at the top states, can be replaced. We will replace that, but not right at this moment. Lastly, the configuration objects passed to the component decorator also specifies where the style sheets for this component are kept. This is done using the style URLs key, and the value for this is an array, so we can pass multiple style sheets. And if we wish, in this case which is passing in one, and that is the app.component.scss file. This is empty at the moment and it's also a file that was created for us automatically by the CLI. The app component class is defined as an export from this file. The class itself is very simple and has just a single property called title, and this is used to display the title of the application. Let's just open up the template once again. So we can see that there is a binding for the title right at the top of the template in the h1 tag. Welcome to {{ title }}. So this Angular's standard interpolations syntax. And we can place simple proxy bindings within them that map to properties, or methods of the component. We'll look at templates much more in detail later in the course, so we'll come back and look at interpolation in much more detail. So that's the example at component which the CLI created for us automatically. So now let's customize things a bit. I don't really having the component files loose in the roots of the application with the module. So first of all, let's create a new folder inside the Apps folder called home. And then let's move all of the app.component files into this new folder. So now let's rename all of these files to be home.component instead of the app.component. And now let's make some changes to the module, because it will still be looking for the old components. So let's open up app.module.ts. And first we'll need to update the path to the component, you can see that there is some red underlining because the path is no longer correct. And let's change the name of the class being imported to home component, instead of app component. And we still got some red underlining here underneath HomeComponent. Because at the moment, the HomeComponent is still exporting a class called app component, so let's just fix that. And still in the home.component file, we'll just need to update the template URL and style URL. Cool. So let's change the selector as well from @python route to @python home. It's a limp rule to prefix app hyphen to the start of all of our component selectors. And now we'll need to updates the custom element in the index page to match the new element that we just defined Having the name of the application defined in code is useful in case we want to change it in some point later on. So let's keep the title proxy in the home component, but let's update it to the name of the app which is going to be js-blackjack. And let's now update the home components template. We'll use this as the shell of our application. So at this stage, it just needs to contain the title for the application. You can keep the h1, and let's just change the text inside it. We don't wanna say welcome to, we'll just print out the name. So the template just contains regular HTML elements and a single binding called title. The binding is contained within double curly braces and it points to the title property in the components class. Angular will evaluate this at run time, and inject the evaluated value from the component into the template. We'll look at bindings and templates in much more detail later in the course. So if we switch back to the command line now, Let's just run the ng serve command. Once the completion has been completed successfully, we should still be able to run the example app in the browser. And we've got that running, let's just refresh that. And now we can see the changes that we have made have been applied. So all we see is the name of the application now and all of the example stuff that the CLI added for us. So in this lesson we looked at components in Angular and saw that they are logical groupings of a view, styling information, data, and behavior. We saw how to use the component decorator to define the component, and we saw how to add a custom element to render the component into. And how to add bindings in the component's template that link back to data in the backing class. Thanks for watching.