Tutorial - Tour of Heroes

Introduction to Angular Basics


Angular CLI

Before you can start developing angular applications you need to install the Angular CLI:

yarn global add @angular/cli

To view the CLI functionality you can use:

ng help

App Initialization

Create a new app with:

ng new my-app-name

And then start the app with:

cd my-app-name
yarn start

You can then open the application on localhost:4200

The App

Starter Files

The Angular applcation that's generated will be found in the src directory with end-to-end tests in the e2e directory

Angular applications are made of different components, the app directory contains the app component which is the main shell of the angular application and consists of the following:

  1. app.component.ts - class
  2. app.component.html - template
  3. app.component.css - styles

The class file contains a property in the class for the title property:


title = 'Tour of Heroes'

Which we can then rendered in the template by replacing its contents with:


The above makes use of the {{title}} notation to render the value of the class' title property

Additionally, the styles.css file contains global styles which will apply to every component


To generate a new component you can use the ng generate component command:

ng generate component

Which will then ask you for a component name. Once the generation is complete it will generate the class, template, and style files for the component as well as add it to the app.module.ts file

The command will also generate a selector property to refer to the component in CSS for the @Component decorator function, this also matches the name to be used for the component's html template from other components

The ngOnInit lifecycle hook is also added, this is what's used to run any initialization logic and is run after the component is created by Angular

The generated class looks like this:


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

  selector: 'app-heroes',
  templateUrl: './heroes.component.html',
  styleUrls: ['./heroes.component.css']
export class HeroesComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {


We can then use this from the app component like so:




You can generate an interface with:

ng generate interface

And then inputting the interface name will generate the interface. Thereafter you can just add properties to the interface as you normally would:


export interface Hero {
  id: number,
  name: string

You can then refer to it in the class like so:

hero: Hero = {
  id: 1,
  name: 'Jeff',

And in a template like so:

<div><span>id: </span>{{hero.id}}</div>
<div><span>name: </span>{{hero.name}}</div>


The Modules are how Angular understands how an application fits together and this allows us to provide some metadata about how an Angular app fits together and what dependencies it has. The top-level AppModule is decorated with NgModule

Furthermode, we can opt-in to other modules from the module file. For example, we can use the FormsModule as follows:


import { FormsModule } from '@angular/forms';

// ...

imports: [

We can the use the FormModule for two-way data binding using an input in the heroes.component.html file:


    <input [(ngModel)]="hero.name" placeholder="name"/>

Which binds the hero.name property between the template and class so that changes on one will reflect on the other. So updating the data in the input will update our class state as well as any other template references



To render a list of elements you can use *ngFor. So to render a list of heroes you can do the following:


<li *ngFor="let hero of heroes">
  <span class="badge">{{hero.id}}</span> {{hero.name}}


You can bind to click events using the click binding with an expression to evaluate on click


<li *ngFor="let hero of heroes" (click)="onSelect(hero)">
  <span class="badge">{{hero.id}}</span> {{hero.name}}

And then implementing a handler in the template:


  onSelect(hero: Hero) {
    this.selectedHerp = hero


You can make use of conditionals with the *ngIf to selectively render a part of the template