Angular Tutorial: ng new

beginner
17 min

Angular Tutorial: ng new

Welcome to CodeYourCraft's in-depth Angular tutorial! Today, we'll be diving into the ng new command – a fundamental step in every Angular project. By the end of this lesson, you'll be well-versed in creating your very own Angular applications. šŸŽÆ

Understanding ng new

ng new is an Angular CLI command that helps you create a new Angular application. It sets up the project structure, installs necessary dependencies, and configures the environment for development. Let's see how it works step-by-step. šŸ“

Prerequisites

  • Node.js (v12 or higher) installed on your system
  • Angular CLI installed globally: npm install -g @angular/cli

Creating Your First Angular Application

To create a new Angular application, navigate to your desired project directory in your terminal and run:

bash
ng new my-first-angular-app

Replace my-first-angular-app with the name you'd like to give your new project. The ng new command will take a few moments to complete. Once done, you'll see a message like this:

bash
Successfully created project my-first-angular-app

Now, let's take a look at the project structure that ng new has created for us. šŸ“

Project Structure

  • my-first-angular-app (root folder)
    • node_modules (contains dependencies)
    • src (source files)
      • app (contains application files)
        • app.module.ts (application module)
        • app.component.ts (default component)
        • app.component.html (default template)
        • app.component.css (default styles)
      • assets (static files like images, fonts, etc.)
      • environments (environment files for development, production, etc.)
    • .angular (Angular specific configuration files)
    • .gitignore (ignored files for Git)
    • package.json (project metadata and dependencies)
    • tsconfig.json (TypeScript configuration)

šŸ’” Pro Tip: Familiarize yourself with this structure as it'll be the foundation for every Angular project you create.

Running Your Application

To run your new Angular application, navigate to the project directory and run:

bash
cd my-first-angular-app ng serve

After a few seconds, you'll see a message:

bash
** NGROK URL: http://127.0.0.1:4200 **

Now, open your web browser and visit the given URL to see your Angular application in action! āœ…

Code Examples

Let's make some changes to the default application to get a feel for how Angular works.

Changing the App Title

  1. Open src/app/app.component.html
  2. Replace the contents of the file with this:
html
<h1>Welcome to My First Angular App!</h1>
  1. Save the file and refresh your browser. You should see the new title displayed.

Adding a Component

  1. In the terminal, run: ng generate component about
  2. Navigate to src/app/about/about.component.ts and make some changes to the code:
typescript
import { Component } from '@angular/core'; @Component({ selector: 'app-about', templateUrl: './about.component.html', styleUrls: ['./about.component.css'] }) export class AboutComponent { title = 'About This App'; }
  1. Open src/app/about/about.component.html and add the following content:
html
<h1>{{ title }}</h1>
  1. Navigate to src/app/app.module.ts and import the newly created AboutComponent:
typescript
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; import { AboutComponent } from './about/about.component'; @NgModule({ declarations: [ AppComponent, AboutComponent ], imports: [ BrowserModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
  1. Save all the files and refresh your browser. You should now see a new link "About" in the navigation bar. Click it to see the new component in action. āœ…

Quiz

Quick Quiz
Question 1 of 1

What is the purpose of the `ng new` command?

That's it for today! You've successfully created and run your first Angular application. In the following lessons, we'll dive deeper into Angular concepts, helping you build powerful, scalable web applications. Stay tuned! šŸŽÆ