📖 Scenario: You are building a simple Angular app for a local library. You want to create a component to show book details.
🎯 Goal: Learn how to create an Angular component using the Angular CLI and set it up in your app.
📋 What You'll Learn
Use Angular CLI to generate a component named book-details
Add a configuration variable showDetails in the component TypeScript file
Use the showDetails variable in the component template to conditionally show book information
Include the book-details component selector in the main app component template
💡 Why This Matters
🌍 Real World
Creating reusable UI parts in Angular apps is common for building interactive web pages.
💼 Career
Knowing how to generate and use components with Angular CLI is a fundamental skill for Angular developers.
Progress0 / 4 steps
1
Generate the book-details component using Angular CLI
Use the Angular CLI command ng generate component book-details to create a new component called book-details.
Angular
Hint
Open your terminal and type ng generate component book-details to create the component files.
2
Add a showDetails boolean variable in book-details.component.ts
In the book-details.component.ts file, add a public boolean variable called showDetails and set it to true.
Angular
Hint
Inside the BookDetailsComponent class, write public showDetails: boolean = true;
3
Use showDetails in the component template to conditionally display book info
In the book-details.component.html file, add an <h2> with text Book Title: Angular Basics that only shows if showDetails is true using Angular's *ngIf directive.
Angular
Hint
Use <h2 *ngIf="showDetails">Book Title: Angular Basics</h2> in the template.
4
Add the book-details component selector to the main app template
In the app.component.html file, add the <app-book-details></app-book-details> tag to display the book details component.
Angular
Hint
Insert <app-book-details></app-book-details> in app.component.html.
Practice
(1/5)
1. What command do you use to create a new Angular component using the CLI?
easy
A. ng build component component-name
B. ng create component component-name
C. ng new component component-name
D. ng generate component component-name
Solution
Step 1: Understand Angular CLI commands
The Angular CLI uses ng generate to create new files and features.
Step 2: Identify the correct syntax for creating components
To create a component, the command is ng generate component followed by the component name.
Final Answer:
ng generate component component-name -> Option D
Quick Check:
Use ng generate component to create components [OK]
Hint: Remember: 'generate' creates, 'create' is incorrect here [OK]
Common Mistakes:
Using 'ng create' instead of 'ng generate'
Confusing 'ng new' which creates a new project
Using 'ng build' which compiles the app
2. Which of the following is the correct syntax to generate a component named header using Angular CLI?
easy
A. ng generate component header
B. ng generate component-header
C. ng generate component:header
D. ng generate component/header
Solution
Step 1: Check the correct command format
The Angular CLI expects the component name as a simple argument after the command.
Step 2: Identify the valid component name format
Component names should be simple words or camelCase without special characters like ':' or '/'.
Final Answer:
ng generate component header -> Option A
Quick Check:
Use space, not symbols, between command and name [OK]
Hint: Use space after 'component' for name, no symbols [OK]
Common Mistakes:
Using symbols like ':' or '/' in component name
Hyphenating the command instead of the name
Confusing command syntax with file paths
3. What files are created when you run ng generate component footer in an Angular project?
medium
A. footer.component.js, footer.component.html, footer.component.css
B. footer.ts, footer.html, footer.css, footer.spec.ts
C. footer.component.ts, footer.component.html, footer.component.css, footer.component.spec.ts
D. footer.component.ts, footer.html, footer.css, footer.spec.js
Solution
Step 1: Recall Angular component file structure
Angular components generate four files: TypeScript, HTML, CSS, and a test spec file, all with the component name prefix.
Step 2: Match the correct file extensions and naming
The files have extensions .ts, .html, .css, and .spec.ts with the component name and '.component' in the filename.
Final Answer:
footer.component.ts, footer.component.html, footer.component.css, footer.component.spec.ts -> Option C
Quick Check:
Angular components create 4 files with .component and correct extensions [OK]