Ir al contenido

Your First Angular Application

Esta página aún no está disponible en tu idioma.

In this guide, you’ll create a simple but complete Angular application featuring:

  • A component that displays a list of items
  • Interactive user input
  • Data binding
  • Event handling
  • Services

After running ng new, your application structure looks like this:

src/
├── app/
│ ├── app.component.ts # Root component
│ ├── app.component.html # Root template
│ ├── app.component.scss # Root styles
│ ├── app.component.spec.ts # Root tests
│ └── app.routes.ts # Route configuration
├── main.ts # Entry point
├── index.html # HTML shell
└── styles.scss # Global styles

The AppComponent is your application’s root:

src/app/app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root', // HTML tag name
standalone: true, // Standalone component (no NgModule needed)
templateUrl: './app.component.html',
styleUrl: './app.component.scss'
})
export class AppComponent {
title = 'My First Angular App';
}
PropertyPurpose
selectorHTML tag name for this component
standaloneComponent doesn’t depend on NgModule (Angular 18+)
template or templateUrlComponent’s HTML (inline or external file)
styles or styleUrlComponent’s CSS (inline or external file)

Generate a component for displaying a list of items:

Terminal window
ng generate component components/item-list

This creates:

src/app/components/item-list/
├── item-list.component.ts
├── item-list.component.html
├── item-list.component.scss
└── item-list.component.spec.ts

Edit item-list.component.ts:

import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
@Component({
selector: 'app-item-list',
standalone: true,
imports: [CommonModule], // Import required modules
templateUrl: './item-list.component.html',
styleUrl: './item-list.component.scss'
})
export class ItemListComponent {
// Component data
items = [
{ id: 1, name: 'Learn Angular', completed: false },
{ id: 2, name: 'Build a component', completed: false },
{ id: 3, name: 'Master services', completed: true }
];
// Component methods
toggleItem(id: number): void {
const item = this.items.find(i => i.id === id);
if (item) {
item.completed = !item.completed;
}
}
addItem(name: string): void {
if (name.trim()) {
const newId = Math.max(...this.items.map(i => i.id), 0) + 1;
this.items.push({ id: newId, name, completed: false });
}
}
removeItem(id: number): void {
this.items = this.items.filter(item => item.id !== id);
}
}

Edit item-list.component.html:

<div class="item-list">
<h2>My Items</h2>
<!-- Input form -->
<div class="add-item">
<input
#itemInput
type="text"
placeholder="Add a new item"
(keyup.enter)="addItem(itemInput.value); itemInput.value = ''"
/>
<button (click)="addItem(itemInput.value); itemInput.value = ''">
Add
</button>
</div>
<!-- Items list -->
<ul *ngIf="items.length > 0; else noItems">
<li *ngFor="let item of items" [class.completed]="item.completed">
<input
type="checkbox"
[checked]="item.completed"
(change)="toggleItem(item.id)"
/>
<span>{{ item.name }}</span>
<button class="delete" (click)="removeItem(item.id)">
Delete
</button>
</li>
</ul>
<!-- Empty state -->
<ng-template #noItems>
<p class="empty">No items yet. Add one to get started!</p>
</ng-template>
</div>
SyntaxPurpose
{{ item.name }}Interpolation (data binding)
[class.completed]="item.completed"Property binding
(click)="removeItem(item.id)"Event binding
[(ngModel)]="item.name"Two-way data binding
*ngIf="items.length > 0"Structural directive (if)
*ngFor="let item of items"Structural directive (loop)
#itemInputTemplate reference variable

Edit item-list.component.scss:

.item-list {
padding: 20px;
max-width: 500px;
margin: 0 auto;
font-family: Arial, sans-serif;
h2 {
color: #333;
margin-bottom: 20px;
}
.add-item {
display: flex;
gap: 10px;
margin-bottom: 20px;
input {
flex: 1;
padding: 8px 12px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 14px;
&:focus {
outline: none;
border-color: #007bff;
box-shadow: 0 0 0 3px rgba(0, 123, 255, 0.1);
}
}
button {
padding: 8px 16px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-weight: 600;
&:hover {
background-color: #0056b3;
}
}
}
ul {
list-style: none;
padding: 0;
margin: 0;
li {
display: flex;
align-items: center;
gap: 12px;
padding: 12px;
border-bottom: 1px solid #eee;
transition: background-color 0.2s;
&:hover {
background-color: #f9f9f9;
}
&.completed {
opacity: 0.6;
span {
text-decoration: line-through;
color: #999;
}
}
input[type="checkbox"] {
cursor: pointer;
}
span {
flex: 1;
}
.delete {
padding: 4px 8px;
background-color: #dc3545;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 12px;
&:hover {
background-color: #c82333;
}
}
}
}
.empty {
color: #999;
text-align: center;
padding: 20px;
font-style: italic;
}
}

Update app.component.ts:

import { Component } from '@angular/core';
import { ItemListComponent } from './components/item-list/item-list.component';
@Component({
selector: 'app-root',
standalone: true,
imports: [ItemListComponent], // Import your component
templateUrl: './app.component.html',
styleUrl: './app.component.scss'
})
export class AppComponent {
title = 'My Task Manager';
}

Update app.component.html:

<div>
<header>
<h1>{{ title }}</h1>
</header>
<main>
<app-item-list></app-item-list>
</main>
</div>
Terminal window
ng serve --open

Your application is now running at http://localhost:4200/!

The selector: 'app-item-list' creates an HTML element <app-item-list></app-item-list>.

<li [class.completed]="item.completed">

Updates the DOM element when the property changes.

<button (click)="removeItem(item.id)">

Responds to user interactions.

<span>{{ item.name }}</span>

Displays component data in the template.

<li *ngFor="let item of items">

Modifies the DOM structure (repeat elements, conditionals).

<input #itemInput />

Access DOM elements in your component code.

// Component
export class MyComponent {
name = 'Angular';
}
<!-- Template -->
<input [(ngModel)]="name" />
<p>Your name: {{ name }}</p>
<!-- Show if items exist -->
<div *ngIf="items.length > 0">
<!-- Content -->
</div>
<!-- Show if items don't exist -->
<div *ngIf="items.length === 0">
No items
</div>
<!-- Using else template -->
<div *ngIf="isLoading; else content">
Loading...
</div>
<ng-template #content>
Content loaded
</ng-template>
<!-- Basic loop -->
<li *ngFor="let item of items">
{{ item.name }}
</li>
<!-- With index -->
<li *ngFor="let item of items; let i = index">
{{ i + 1 }}. {{ item.name }}
</li>
<!-- With tracking (performance) -->
<li *ngFor="let item of items; trackBy: trackByItemId">
{{ item.name }}
</li>
trackByItemId(index: number, item: Item): number {
return item.id;
}

Now that you understand the basics:

  1. Create more components — Break your UI into smaller parts
  2. Learn about services — Share data between components
  3. Explore routing — Create multi-page applications
  4. Master forms — Build complex forms with validation
  5. Connect to APIs — Fetch data from backend servers