This tutorial lesson demonstrates how to create a new component for your Angular app.
What you'll learn
Your app has a new component: Home.
Conceptual preview of Angular components
Angular apps are built around components, which are Angular's building blocks. Components contain the code, HTML layout, and CSS style information that provide the function and appearance of an element in the app. In Angular, components can contain other components. An app's functions and appearance can be divided and partitioned into components.
In Angular, components have metadata that define its properties.
When you create your Home, you use these properties:
selector: to describe how Angular refers to the component in templates.standalone: to describe whether the component requires aNgModule.imports: to describe the component's dependencies.template: to describe the component's HTML markup and layout.styleUrls: to list the URLs of the CSS files that the component uses in an array.
-
Create the
HomeIn this step, you create a new component for your app.
In the Terminal pane of your IDE:
In your project directory, navigate to the
first-appdirectory.Run this command to create a new
Homeng generate component homeRun this command to build and serve your app.
NOTE: This step is only for your local environment!
ng serveOpen a browser and navigate to
http://localhost:4200to find the application.Confirm that the app builds without error.
HELPFUL: It should render the same as it did in the previous lesson because even though you added a new component, you haven't included it in any of the app's templates, yet.
Leave
ng serverunning as you complete the next steps.
-
Add the new component to your app's layout
In this step, you add the new component,
Hometo your app's root component,App, so that it displays in your app's layout.In the Edit pane of your IDE:
Open
app.tsin the editor.In
app.ts, importHomeby adding this line to the file level imports.Import Home in src/app/app.ts
import {Component} from '@angular/core';import {Home} from './home/home';@Component({ selector: 'app-root', imports: [Home], template: ` <main> <header class="brand-name"> <img class="brand-logo" src="/assets/logo.svg" alt="logo" aria-hidden="true" /> </header> <section class="content"> <app-home></app-home> </section> </main> `, styleUrls: ['./app.css'],})export class App { title = 'homes';}In
app.ts, in@Component, update theimportsarray property and addHome.Replace in src/app/app.ts
import {Component} from '@angular/core';import {Home} from './home/home';@Component({ selector: 'app-root', imports: [Home], template: ` <main> <header class="brand-name"> <img class="brand-logo" src="/assets/logo.svg" alt="logo" aria-hidden="true" /> </header> <section class="content"> <app-home></app-home> </section> </main> `, styleUrls: ['./app.css'],})export class App { title = 'homes';}In
app.ts, in@Component, update thetemplateproperty to include the following HTML code.Replace in src/app/app.ts
import {Component} from '@angular/core';import {Home} from './home/home';@Component({ selector: 'app-root', imports: [Home], template: ` <main> <header class="brand-name"> <img class="brand-logo" src="/assets/logo.svg" alt="logo" aria-hidden="true" /> </header> <section class="content"> <app-home></app-home> </section> </main> `, styleUrls: ['./app.css'],})export class App { title = 'homes';}Save your changes to
app.ts.If
ng serveis running, the app should update. Ifng serveis not running, start it again. Hello world in your app should change to home works! from theHome.Check the running app in the browser and confirm that the app has been updated.

-
Add features to
HomeIn this step you add features to
Home.In the previous step, you added the default
Hometo your app's template so its default HTML appeared in the app. In this step, you add a search filter and button that is used in a later lesson. For now, that's all thatHomehas. Note that, this step just adds the search elements to the layout without any functionality, yet.In the Edit pane of your IDE:
In the
first-appdirectory, openhome.tsin the editor.In
home.ts, in@Component, update thetemplateproperty with this code.Replace in src/app/home/home.ts
import {Component} from '@angular/core';import {CommonModule} from '@angular/common';@Component({ selector: 'app-home', imports: [CommonModule], template: ` <section> <form> <input type="text" placeholder="Filter by city" /> <button class="primary" type="button">Search</button> </form> </section> `, styleUrls: ['./home.css'],})export class Home {}Next, open
home.cssin the editor and update the content with these styles.NOTE: In the browser, these can go in
src/app/home/home.tsin thestylesarray.Replace in src/app/home/home.css
.results { display: grid; column-gap: 14px; row-gap: 14px; grid-template-columns: repeat(auto-fill, minmax(400px, 400px)); margin-top: 50px; justify-content: space-around;}input[type="text"] { border: solid 1px var(--primary-color); padding: 10px; border-radius: 8px; margin-right: 4px; display: inline-block; width: 30%;}button { padding: 10px; border: solid 1px var(--primary-color); background: var(--primary-color); color: white; border-radius: 8px;}@media (min-width: 500px) and (max-width: 768px) { .results { grid-template-columns: repeat(2, 1fr); } input[type="text"] { width: 70%; } }@media (max-width: 499px) { .results { grid-template-columns: 1fr; } }Confirm that the app builds without error. You should find the filter query box and button in your app and they should be styled. Correct any errors before you continue to the next step.

SUMMARY: In this lesson, you created a new component for your app and gave it a filter edit control and button.
For more information about the topics covered in this lesson, visit: