Web Dev Academy JavaScript frameworks · Angular Tool 14 / 64
JavaScript frameworks

Angular

A complete, batteries-included framework from Google. Angular brings components, two-way binding, dependency injection and a CLI-driven build into one opinionated platform.

!
Angular apps are compiled with the Angular CLI (TypeScript + Ahead-of-Time templates) and can't be dropped in via a single CDN tag for a realistic demo. The interactive demos below are a labelled vanilla-JS simulation of Angular's behaviour, and each code block shows the real Angular component & template code.
Demo 01

Two-way binding with [(ngModel)]

Angular's "banana-in-a-box" syntax [(ngModel)] binds an input to a component property in both directions. Type to see it update.

● Simulation

Welcome, stranger!

// app.component.ts
@Component({
  selector: 'app-root',
  template: `
    <input [(ngModel)]="name">
    <p>Welcome, {{ name || 'stranger' }}!</p>
  `
})
export class AppComponent {
  name = '';
}
Demo 02

Rendering lists with *ngFor

The *ngFor structural directive stamps out one element per item in a collection. Add and remove tasks below.

● Simulation
    <li *ngFor="let task of tasks; let i = index">
      {{ i + 1 }}. {{ task }}
      <button (click)="remove(i)">×</button>
    </li>
    Demo 03

    Event binding with (click)

    Wrap an event name in parentheses to bind it. Here a counter increments through (click) calling a component method.

    ● Simulation
    Items: 0
    <button (click)="addItem()">Add to cart</button>
    <span>Items: {{ items }}</span>
    
    // component
    addItem() { this.items++; }
    Demo 04

    Services & dependency injection

    Angular's killer feature: a @Injectable service holds shared logic, and components inject it through their constructor. Both badges read the same injected counter service.

    ● Simulation
    Header: 0
    Sidebar: 0
    @Injectable({ providedIn: 'root' })
    export class CounterService {
      count = 0;
      increment() { this.count++; }
    }
    
    @Component({ ... })
    export class HeaderComponent {
      constructor(public counter: CounterService) {}
    }
    i
    The demos on this page are a vanilla-JavaScript simulation of Angular concepts ([(ngModel)] two-way binding, *ngFor lists, event binding and a shared injectable service) because Angular needs a CLI build step. Every code block shows the real Angular component & template source. The shared CSS shell provides page styling.

    ↑ All 64 tools