3d cartoon hands holding a phone

Unlock full course by purchasing a membership

Lesson 5

Working with Components in Angular

Understanding one of the key building blocks of Angular applications

STANDARD

Working with Components in Angular

We discussed a bunch of different decorators in the last lesson, but the decorator that we will be using far more than any other is the @Component decorator to create custom components. We are going to spend this lesson discussing some core concepts of working with components.

We can think of components as the fundamental building blocks of an Angular application. We have had a glimpse of this already — our entire application is built of components within components within components.

A Basic Component

We have already built some basic components that look like this:

import { Component } from '@angular/core';

@Component({
  standalone: true,
  selector: 'app-welcome',
  template: ` <p>Hi, Josh!</p> `,
})
export class WelcomeComponent {}

and these components can then be added to another components template like this:

import { Component } from '@angular/core';
import { WelcomeComponent } from './ui/welcome.component';

@Component({
  standalone: true,
  selector: 'app-home',
  template: `
    <app-welcome />
    <p>I am the home component</p>
  `,
  imports: [WelcomeComponent]
})
export class HomeComponent {}

or they can be routed to and displayed with a <router-outlet>.

This example is about as basic as a component will get. The reason we might create a component like this is just to help modularise our application — all we are really doing is displaying a template and binding some simple data. We will talk more about architecture concepts later, but the general idea is that it is better to have lots of components each doing some small/targeted thing, rather than having components that try to do too much.

However, there are two fundamental concepts of Angular components we are missing here that help control how parent and child components relate to each other: input and output.

If we put ComponentB inside of the template of ComponentA:

@Component({
    selector: 'component-a',
    template: `<component-b></component-b>`
})
export class ComponentA {}

Then we would say that ComponentA is the parent and ComponentB is the child. The key idea is that a parent component can communicate with a child component by providing it with an input. A child component can communicate with a parent component by providing it with an output.

Input and Output

Let’s investigate how inputs and outputs work in more detail.

@Input

NOTE: There is a new syntax coming for both inputs and outputs, but this is not yet available.

We are going to switch back to the more real example of our WelcomeComponent which currently lives within the template of the HomeComponent.

Now let’s imagine that we want to display a greeting for a particular user’s name, not just Josh. As we will discuss later, we generally don’t want our child/dumb components to have to deal with any complex business logic or data fetching. So, what we might want to do is have our parent component provide the child component with an input so that it knows what name to display:

import { Component } from '@angular/core';
import { WelcomeComponent } from './ui/welcome.component';

@Component({
  standalone: true,
  selector: 'app-home',
  template: `
    <app-welcome [name]="user.name" />
    <p>I am the home component</p>
  `,
  imports: [WelcomeComponent],
})
export class HomeComponent {
  user = {
    name: 'Josh',
  };
}

We are still just using a static value of Josh here, but imagine that our HomeComponent might pull this user value in from some kind of UserService that will return the logged in user.

STANDARD
Key

Thanks for checking out the preview of this lesson!

You do not have the appropriate membership to view the full lesson. If you would like full access to this module you can view membership options (or log in if you are already have an appropriate membership).