Video lecture
Watch on YouTube(opens in a new tab)Sub topic 1
What Angular Is and When to Choose It
Angular is a component-based web application framework from Google, written in TypeScript. A view-only library such as React handles rendering and leaves routing, data fetching and builds to other packages. Angular includes routing, dependency injection, forms, an HTTP client, testing setup and build tooling in one versioned platform. Choose it when a large team needs one consistent structure. It also fits apps with many screens, or projects where you want the framework to make most of the setup decisions. For a small widget, it may be more than you need.
Prerequisites
This chapter assumes you know HTML, CSS and modern JavaScript (ES modules, classes and arrow functions). It also assumes basic TypeScript types, the decorator syntax, and comfort with a terminal and npm. You need Node.js installed, using an active or maintenance LTS release.
Why Angular Uses TypeScript
Angular reads metadata from decorators such as @Component. It also uses static types to check templates and inject dependencies. TypeScript catches wrong input types and misspelled properties at compile time instead of in the browser. The CLI configures the compiler for you, so you can start writing .ts files without setting anything up first.
Installing and Using the Angular CLI
The Angular CLI (ng) creates projects, generates code, serves, tests and builds your app.
npm install -g @angular/cli
ng version
ng new my-app
cd my-app
ng serve --openng new asks you to pick a stylesheet format and whether to enable server-side rendering. It then installs the dependencies. ng serve --open builds the app, opens it at http://localhost:4200, and rebuilds whenever you save a file.
Touring the Generated Project
| File | Purpose |
|---|---|
| `angular.json` | Workspace configuration for the build, serve and test commands |
| `package.json` | npm dependencies and scripts |
| `tsconfig.json` | TypeScript compiler options |
| `src/index.html` | Host page that contains the root element |
| `src/main.ts` | Entry point that bootstraps the app |
| `src/styles.css` | Global styles |
| `src/app/app.ts` | Root component (`app.component.ts` in older CLI versions) |
| `src/app/app.config.ts` | Application-wide providers |
| `src/app/app.routes.ts` | Route definitions |
| `public/` | Static assets copied as-is |
Exercise
Your team needs routing, forms and an HTTP client with a single upgrade path. Which file would you open to register application-wide providers, and why does Angular suit this team better than a view-only library?
Show answer
Open src/app/app.config.ts. Angular ships routing, forms and HTTP as one versioned platform. The team gets those features without choosing and upgrading separate third-party libraries.