- Open your terminal or command prompt.
- Install the Angular CLI globally by running the following command:
npm install -g @angular/cli
- Verify the installation by checking the version:
ng version
- You should see the installed version of Angular CLI displayed in the output.
-
Create a new Angular project by running the following command:
ng new my-first-app
-
You will be prompted to answer a few questions:
- Would you like to add Angular routing? (Select Yes or No depending on your preference)
- Choose a stylesheet format (Select CSS for now).
-
Once the project is created, navigate into the project directory:
cd my-first-app
-
You can now open the folder / project in Visual Studio Code by typing the following command:
code .
- Open a cmd terminal in Visual Studio Code.
Make sure you use a cmd terminal and not a powershell terminal
- Start the development server using the command:
ng serve
- Open a web browser and navigate to
http://localhost:4200
. You should see the default Angular welcome page.
- Open the project in your preferred code editor.
- Navigate to the
src/app/app.component.html
file. - Delete all the content within the components template (the default HTML content).
- Save the file.
- Notice that in the browser you now have an empty page
- Navigate to
src/app/app.component.ts
. - Inside the
AppComponent
class, add a new property calledcaption
and assign a string value to it:export class AppComponent { caption = 'My First Angular App'; }
- Go back to the
app.component.html
file. - Use Angular's interpolation to display the value of the
title
property:<h1>{{ caption }}</h1>
- Save the file, and the browser should automatically reload with the updated content. You should now see the title displayed on the page.
- You have successfully created a basic Angular project, modified the app component, and displayed a property from the component's TypeScript file in the HTML.
In this exercise, you will continue working on the project created in the previous exercise. You will practice handling button and input events, modifying the state of the component, and displaying information dynamically on the screen.
- Open
src/app/app.component.html
. - Add a button element:
<button (click)="onButtonClick()">Click me</button>
-
Open
src/app/app.component.ts
. -
Add a new method to the
AppComponent
class that will be triggered when the button is clicked:export class AppComponent { caption = 'My First Angular App'; onButtonClick() { console.log('Button clicked!'); this.caption = 'Button Clicked!'; } }
-
Save the file and check the browser. When you click the button, the title should change to "Button Clicked!" and the message will be logged to the console.
- In
src/app/app.component.html
, add an input field and bind its input event to a method:<input type="text" #textBox (input)="onInputChange(textBox.value)" placeholder="Type something" />
-
In
src/app/app.component.ts
, add a method to handle the input event:export class AppComponent { caption = 'My First Angular App'; inputText = ''; onButtonClick() { this.caption = 'Button Clicked!'; } onInputChange(value: string) { this.inputText = value; } }
-
This method updates the
inputText
property whenever the user types in the input field.
-
In
src/app/app.component.html
, display the value of theinputText
property below the input field:<p>You typed: {{ inputText }}</p>
-
Save the changes and check the browser. As you type into the input field, the text you type should be displayed dynamically on the screen.
- You have successfully connected button and input events to methods, modified the component's state, and displayed information dynamically on the screen.
In this exercise, you will use Angular’s new control flow (@if
and @for
) to control the visibility of elements and display lists dynamically based on component data.
- Open
src/app/app.component.html
. - Add a button that toggles the visibility of a message using
@if
:<button (click)="toggleMessage()">Toggle Message</button> @if (showMessage) { <p>This is a conditional message!</p> }
- Open
src/app/app.component.ts
. - Add a property
showMessage
and a methodtoggleMessage
to theAppComponent
class to toggle the visibility of the message:export class AppComponent { caption = 'My First Angular App'; showMessage = false; toggleMessage() { this.showMessage = !this.showMessage; } }
- Save the file. In the browser, you should now be able to toggle the visibility of the message by clicking the button.
- In
src/app/app.component.ts
, add an array of strings to represent a list of items:export class AppComponent { title = 'My First Angular App'; showMessage = false; items = ['Item 1', 'Item 2', 'Item 3', 'Item 4']; toggleMessage() { this.showMessage = !this.showMessage; } }
- In
src/app/app.component.html
, add an unordered list that uses@for
to iterate over theitems
array and display each item:<ul> @for (item of items; track item) { <li>{{ item }}</li> } </ul>
- You have successfully used
@if
to conditionally display content and@for
to iterate over and display a list of items dynamically in your Angular app.
In this exercise, you will practice using Angular’s [class.xxx]
and [style.xxx]
directives to dynamically apply CSS classes and styles based on component data.
-
Open
src/app/app.component.html
. -
Add a button that will dynamically apply a CSS class when clicked:
<button (click)="toggleHighlight()" [class.highlight]="isHighlighted">Toggle Highlight</button>
-
Define the
highlight
class in thesrc/styles.css
file (or any global stylesheet):.highlight { background-color: yellow; color: black; }
- Open
src/app/app.component.ts
. - Add a property
isHighlighted
and a methodtoggleHighlight
to control the button’s appearance:export class AppComponent { isHighlighted = false; toggleHighlight() { this.isHighlighted = !this.isHighlighted; } }
- Save the file and check the browser. When you click the button, it should toggle the
highlight
class, changing the button’s background color.
- In
src/app/app.component.html
, add a paragraph that dynamically changes its text color based on a component property:<p [style.color]="textColor">This text will change color dynamically.</p> <button (click)="changeColor()">Change Text Color</button>
-
In
src/app/app.component.ts
, add a propertytextColor
and a methodchangeColor
to update the style dynamically:export class AppComponent { isHighlighted = false; textColor = 'black'; toggleHighlight() { this.isHighlighted = !this.isHighlighted; } changeColor() { this.textColor = this.textColor === 'black' ? 'blue' : 'black'; } }
-
Save the file and check the browser. When you click the "Change Text Color" button, the text color should toggle between black and blue.
- You have successfully used
[class.xxx]
to conditionally apply CSS classes and[style.xxx]
to dynamically apply inline styles based on component properties.
In this exercise, you will convert the component’s state management from traditional properties to Angular signals. You will practice working with signals for state management and see how they integrate into the component’s lifecycle.
- Open
src/app/app.component.ts
. - Replace the traditional
isHighlighted
property with a signal:import { signal } from '@angular/core'; export class AppComponent { isHighlighted = signal(false); toggleHighlight() { this.isHighlighted.update(value => !value); } }
Update the app.component.html
to work with the signal:
<button (click)="toggleHighlight()" [class.highlight]="isHighlighted()">Toggle Highlight</button>
Now, in the same AppComponent
, convert the textColor
property to a signal:
export class AppComponent {
isHighlighted = signal(false);
textColor = signal('black');
toggleHighlight() {
this.isHighlighted.update(value => !value);
}
changeColor() {
this.textColor.update(color => color === 'black' ? 'blue' : 'black');
}
}
Update the HTML to bind to the textColor
signal:
<p [style.color]="textColor()">This text will change color dynamically.</p>
<button (click)="changeColor()">Change Text Color</button>
You have successfully converted your component’s state to use Angular signals for both class binding and dynamic styling.
In this exercise, you will extend the state management of the previous exercise by adding a computed signal. A computed signal will be used to derive new values based on other signals in the component.
- Open
src/app/app.component.ts
. - Add a new computed signal based on the
isHighlighted
andtextColor
signals:import { signal, computed } from '@angular/core'; export class AppComponent { isHighlighted = signal(false); textColor = signal('black'); // Computed signal that generates a message based on the highlight and color states statusMessage = computed(() => { const highlightStatus = this.isHighlighted() ? 'Highlighted' : 'Not Highlighted'; return `The button is ${highlightStatus} and the text color is ${this.textColor()}`; }); toggleHighlight() { this.isHighlighted.update(value => !value); } changeColor() { this.textColor.update(color => color === 'black' ? 'blue' : 'black'); } }
- Open
src/app/app.component.html
. - Add a new paragraph to display the computed
statusMessage
:<p>{{ statusMessage() }}</p>
- Run the application and test the behavior. When you click the button to toggle the highlight or change the text color, the status message should update automatically.
- You have successfully added a computed signal to derive new values based on other signals in the component.