DotNet-301-ElevenNoteAngular
  • Introduction
  • Part 1: Setup with the Angular CLI
  • Part 2: Building out the Header Component
  • Part 3: Sprucing up the Header Using Material
  • Part 4: Building the Registration Page with Reactive Angular Form
  • Part 5: Routing with Angular Router
  • Part 6: Design Break: Fonts
  • Part 7: Communicating with Web API with Auth Service
  • Part 8: Logging in with a token
  • Part 9: Routing After a Request
  • Part 10: Log In page Challenge
  • Part 11: Building the Note Index page
  • Part 12: Creating Note C.r.u.d. — Create
  • Part 13: Creating Note c.R.u.d. — Read
  • Part 14: Creating Note c.r.U.d. — Update
  • Part 15: Creating Note c.r.u.D. — Delete
  • Part 16: Protecting our views with Guards
Powered by GitBook
On this page
  • Step 1. Create a component file
  • Step 2. Generate header component
  • Step 3. Delete the Spec file
  • Step 4. Showing our Component

Part 2: Building out the Header Component

PreviousPart 1: Setup with the Angular CLINextPart 3: Sprucing up the Header Using Material

Last updated 7 years ago

In this module we will be focusing on building out the angular component using the Angular CLI.

Step 1. Create a component file

We want to organize our application so that we can have a hub for all of our components to be stored.

Inside of the app folder, create a new folder called components.

This is where we will be building all of our components from here on out.

Step 2. Generate header component

In the Terminal, let’s build out the first component — Make sure you are in your application directory!

Type:

ng generate component components/header

That command scaffolds out a component in the file location that we include at the end of the file by saying “components/header”.

We should have a header component in our components folder now.

When we use the Angular scaffold it gives us four files.

But also it modifies another file, the app.module.ts. It Includes our newly created component into the declarations key as one of the array properties. Notice that this module has been added:

Step 3. Delete the Spec file

The Spec file is used for unit testing. It is something we'll want to do, but for now, it is out of scope for this application. So we are going to delete that file (header.component.spec.ts) for now, and actually we will see how we can generate our future components without generating the test file.

Step 4. Showing our Component

Now that we have our component, how do we make it show on our web page?

Let’s take a look at the header.component.ts file, the start of our answer begins there.

In lines 3 - 7 we have this Component Decorator. This decorator houses the settings of our header component.

Selector— This is what Angular will know our component as if we include it in other places in our application.

TemplateUrl— This is how our html file is connected to this component. This connection is very important, because this is how we will exchange data from our view (html file) to this one.

StyleUrls— This is how we also connect CSS files to the html page. *Notice the square brackets, that indicates that we can include multiple stylesheets!

Let’s get to seeing our component on a web page.

First, run the application with:

ng serve -o

(before we used --open, but now we are using the shorthand)

We should just have an awesomely blank page (that’s why there isn’t an image, ain’t nobody have time for that)

Keeping the browser open in the background. Let’s show our component!

In the app.component.html, add

<app-header> </app-header>

Now, after saving it navigate back to the browser and we should see this:

<app-header> </app-header>

is known as a Component Directive.

Notice, that component directives look like a markup language that we are familiar with, can you guess… that’s right HTML!

With our Angular we are making our own customized HTML tags.

We have finished building out the header component! Before we make it look like a header, let’s include our style UI library Material.

Logo Title Text 1
Logo Title Text 1
Logo Title Text 1
Logo Title Text 1
Logo Title Text 1
Logo Title Text 1