DEV Community

Cover image for Your First Angular App: A Beginner's Guide to Getting Started
Satyam Gupta
Satyam Gupta

Posted on

Your First Angular App: A Beginner's Guide to Getting Started

Your First Angular App: A Beginner's Guide to Getting Started

So, you've heard the buzz about Angular. Your developer friends won't stop talking about it, job listings are full of it, and you're ready to see what the fuss is all about. You're in the right place. Building your first Angular app can feel like a daunting task, but I promise, it's an incredibly rewarding one.

Think of Angular as a powerful toolkit for building structured, dynamic, and scalable web applications. It's not just a library; it's a full-fledged platform. In this guide, we're going to walk through creating your very first Angular application together. We'll start from the absolute beginning, demystify the core concepts, and build a simple, functional app. No prior Angular knowledge is required—just a basic understanding of HTML, CSS, and JavaScript will do.

Let's roll up our sleeves and build something.

What is Angular, Anyway?
Before we dive into code, let's get our definitions straight. Angular is an open-source, front-end web application framework led by the Angular Team at Google. Its primary strength lies in building Single-Page Applications (SPAs).

What's a SPA? It's a web application that loads a single HTML page and dynamically updates that page as the user interacts with the app, instead of loading entire new pages from the server. Think Gmail, Google Docs, or Facebook. The experience is fluid and fast, much like a desktop application.

Angular achieves this by using a component-based architecture. You build your app out of reusable, self-contained pieces called Components. Each component controls a patch of screen called a view.

The Core Building Blocks We'll Use Today
Components: The fundamental building blocks. A component includes a TypeScript class for logic, an HTML template for the view, and a CSS file for styles.

Modules (NgModule): Angular apps are modular. The AppModule is the root module that bootstraps and launches the application. It's like a container for related pieces of your app.

Templates: The HTML part of your component, supercharged with Angular syntax like data binding.

Data Binding: The magic that connects your application data (in the TypeScript class) with the DOM (the HTML template). When the data changes, the view updates automatically.

Setting the Stage: Prerequisites
To work with Angular, you'll need Node.js and npm (the Node Package Manager) installed on your machine. They come together. You can download them from nodejs.org. Once installed, open your terminal or command prompt and verify:

bash
node --version
npm --version
If you see version numbers, you're good to go!

Step 1: Installing the Angular CLI
The Angular CLI (Command Line Interface) is your best friend. It's a powerful tool that automates your development workflow. It can create a new project, generate application code, perform tests, and build your app for production.

Install it globally using npm:

bash
npm install -g @angular/cli
This might take a minute. Go grab a coffee.

Step 2: Creating Your New Project
Now for the exciting part! Let's create a new Angular project. We'll call it my-first-app.

bash
ng new my-first-app
The CLI will ask you a couple of questions:

Would you like to add Angular routing? Type y for yes. (Routing is how we navigate between different views in our SPA).

Which stylesheet format would you like to use? For simplicity, select CSS.

The CLI will now whip up a brand new, fully configured Angular project. It will install all the necessary dependencies, which, again, might take a moment.

Once it's done, navigate into your project folder:

bash
cd my-first-app
Step 3: Serving Your Application
You're now ready to see your app in action! From inside the project directory, run:

bash
ng serve
The CLI will build your application and make it available on a local development server. Open your browser and go to . Voilà! You should see the default Angular welcome page, which means your environment is set up perfectly.

Pro Tip: The ng serve command watches your source files. If you make any changes and save them, the browser will automatically reload to reflect the updates. This is called live-reload, and it's a huge productivity booster.

Step 4: Understanding the Project Structure
Let's quickly open the project in a code editor (like VS Code). You'll see a structure like this:

text

src/
├── app/
│   ├── app.component.css
│   ├── app.component.html
│   ├── app.component.ts
│   └── app.module.ts
├── assets/
├── environments/
├── index.html
└── main.ts
Here's the breakdown of the key files:
Enter fullscreen mode Exit fullscreen mode

app.component.ts: The TypeScript class for your main root component.

app.component.html: The HTML template for the root component.

app.component.css: The styles for the root component.

app.module.ts: The root module that tells Angular how to assemble the application.

main.ts: The main entry point that bootstraps (starts) the AppModule.

Let's Build: A Simple Task List
Reading is good, but doing is better. Let's modify our app to create a very simple task list. This will introduce you to key concepts like data binding, components, and directives.

  1. Updating the App Component (app.component.ts) First, we'll define our application's data and logic. Open app.component.ts and replace its contents with the following:

typescript

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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'My First Angular App: Task List';
  newTask: string = '';
  tasks: string[] = ['Learn the basics of Angular', 'Build my first app', 'Celebrate!'];

  addTask() {
    if (this.newTask.trim()) {
      this.tasks.push(this.newTask);
      this.newTask = ''; // Clear the input field
    }
  }

  removeTask(index: number) {
    this.tasks.splice(index, 1);
  }
}
Enter fullscreen mode Exit fullscreen mode

What did we just do?

We changed the title property to something more relevant.

We added a newTask property to hold the value of the new task input field.

We created a tasks array, pre-populated with a few items.

We defined an addTask() method to push a new task into the array.

We defined a removeTask() method to delete a task based on its index.

  1. Building the Template (app.component.html) Now, let's create the view that will display our tasks and allow user interaction. Replace everything in app.component.html with this:

html

<div class="container">
  <h1>{{ title }}</h1>

  <!-- Input for adding a new task -->
  <div class="input-section">
    <input
      type="text"
      placeholder="Add a new task..."
      [(ngModel)]="newTask"
      (keyup.enter)="addTask()"
    >
    <button (click)="addTask()">Add Task</button>
  </div>

  <!-- List to display tasks -->
  <ul class="task-list">
    <li *ngFor="let task of tasks; let i = index" class="task-item">
      <span>{{ task }}</span>
      <button (click)="removeTask(i)" class="delete-btn">Delete</button>
    </li>
  </ul>

  <p *ngIf="tasks.length === 0">Hooray! All tasks are done. Time to add more.</p>
</div>
Enter fullscreen mode Exit fullscreen mode

This is where the magic happens! Let's break down the Angular features:

Interpolation {{ title }}: This displays the value of the component's title property in the HTML.

Two-Way Data Binding [(ngModel)]="newTask": This is a powerful directive that keeps the component's newTask property and the input field's value in sync. When you type, the property updates, and vice-versa. (Note: To use ngModel, you need to import the FormsModule in your app.module.ts).

Event Binding (click)="addTask()": This calls the component's addTask() method when the button is clicked. Similarly, (keyup.enter)="addTask()" calls the method when the "Enter" key is pressed in the input field.

Structural Directive *ngFor="let task of tasks": This is a loop. It creates one

  • element for each task in the tasks array.

    Structural Directive *ngIf="tasks.length === 0": This conditionally shows the paragraph only if the tasks array is empty.

    1. A Touch of Style (app.component.css) Let's make it look presentable. Add some styles to app.component.css:
    css
    .container {
      max-width: 600px;
      margin: 50px auto;
      font-family: Arial, sans-serif;
    }
    
    .input-section {
      margin-bottom: 20px;
    }
    
    input[type="text"] {
      padding: 10px;
      width: 70%;
      margin-right: 10px;
      border: 1px solid #ddd;
      border-radius: 4px;
    }
    
    button {
      padding: 10px 15px;
      background-color: #007bff;
      color: white;
      border: none;
      border-radius: 4px;
      cursor: pointer;
    }
    
    button:hover {
      background-color: #0056b3;
    }
    
    .task-list {
      list-style: none;
      padding: 0;
    }
    
    .task-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 10px;
      border-bottom: 1px solid #eee;
    }
    
    .delete-btn {
      background-color: #dc3545;
    }
    
    .delete-btn:hover {
      background-color: #c82333;
    }
    
    1. A Quick Fix: Importing the FormsModule Remember the ngModel? We need to tell Angular about it. Open app.module.ts and add the following import, and include FormsModule in the imports array:
    typescript
    import { NgModule } from '@angular/core';
    import { BrowserModule } from '@angular/platform-browser';
    import { FormsModule } from '@angular/forms'; // <-- Import FormsModule
    
    import { AppComponent } from './app.component';
    
    @NgModule({
      declarations: [
        AppComponent
      ],
      imports: [
        BrowserModule,
        FormsModule // <-- Include it here
      ],
      providers: [],
      bootstrap: [AppComponent]
    })
    

    export class AppModule { }
    Now, go back to your browser (it should have updated automatically thanks to ng serve). You have a working task list! You can add new tasks and delete existing ones. You've just built a dynamic, interactive Angular application.

    Real-World Use Cases for Angular
    This simple task list is a microcosm of what Angular can do. In the real world, Angular is used to build:

    Enterprise Web Applications: Large-scale internal dashboards for data management and visualization.

    Dynamic Content Portals: News websites, video streaming platforms, and social media feeds.

    E-commerce Platforms: Product catalogs, shopping carts, and user account management systems.

    Single-Page Applications (SPAs): Any application where a seamless, app-like user experience is crucial.

    Mastering Angular opens doors to building complex, high-performance web applications that are in high demand. If you're serious about a career in modern web development, a deep understanding of frameworks like Angular is non-negotiable. To learn professional software development courses such as Python Programming, Full Stack Development, and MERN Stack, visit and enroll today at codercrafter.in. Our structured curriculum is designed to take you from beginner to job-ready.

    Best Practices to Grow Into
    As you continue your Angular journey, keep these in mind:

    Component Reusability: Design components to be as generic and reusable as possible.

    Services for Logic: Move data-fetching and business logic into Services instead of keeping it in components.

    Use TypeScript: Embrace TypeScript's strong typing. It will save you from countless bugs.

    Lazy Loading: For large apps, use lazy loading for feature modules to improve initial load time.

    Frequently Asked Questions (FAQs)
    Q1: Is Angular better than React?
    A: This is the age-old debate. Neither is objectively "better." Angular is a full-fledged framework with more built-in solutions, while React is a library that gives you more flexibility in choosing other tools. Angular is often preferred for large, enterprise-level applications due to its structure and consistency.

    Q2: Do I need to know TypeScript to learn Angular?
    A: It's highly recommended. Angular is built with TypeScript, and most of its documentation and examples use it. While it's possible to use JavaScript, you'll miss out on many of Angular's productivity and maintainability benefits.

    Q3: How do I deploy my Angular app?
    A: You can build it for production using ng build --prod. This creates a dist/ folder with optimized, minified files that you can host on any static web hosting service like Netlify, Vercel, Firebase Hosting, or a traditional web server.

    Conclusion: You've Just Begun
    Congratulations! You've successfully set up your development environment, learned the core concepts of Angular, and built a functional, interactive application. You've seen how data binding connects your logic to your view, how directives manipulate the DOM, and how components form the heart of an Angular app.

    This is just the first step on a thrilling path. The world of Angular is vast, with much more to explore: services, dependency injection, routing, HTTP client, forms, and state management. The best way to learn is to keep building. Add features to your task list. Change the styling. Break things and then fix them.

    The journey to becoming a proficient developer is challenging but incredibly rewarding. If you found this guide helpful and are looking for a structured path to master not just Angular, but the entire landscape of web development, we have just the thing for you. To learn professional software development courses such as Python Programming, Full Stack Development, and MERN Stack, visit and enroll today at codercrafter.in. Let's build the future, one line of code at a time.

  • Top comments (0)