©2017 by Michael Duquette. Proudly created with

  • Michael Duquette

…and we’re back! Last week we created the get-all-employees component and the employee class. This week we’ll finish up by modifying:

  • get-all-employees.component.html

  • app.component.html

  • app.modules.ts

  • app.component.ts

Before we move on open up the get-all-employees.component.ts and make sure you have an import statement for the Employee class up top. It should look like this:

import {Employee} from ‘../employee’;

If it’s not there please add it. Ok now moving right along…

Once we touch up these four files you’ll have a working RestAPI Get call on your page. So let’s get to it and wrap this up! Open the get-all-employees.component.html – this defines how each employee is displayed. Modify using this code:

<div class="content">


<fieldset style="width:90%">

<legend>Get All Employee</legend>


<button (click)="getAllEmployee()">

Get all employees



<li *ngFor="let employee of employees">

Employee id:{{}}<br>

Employee Name:{{employee.employee_name}}<br>

Employee Salary: {{employee.employee_salary}}<br>

Employee Age: {{employee.employee_age}}<br><br>



<ul *ngIf="error">








Let’s break down what we did here. We created a small form that has a button and two lists. The button, when clicked, calls our getAllEmployee() method which populates our employees Array. We handle populating the employee list on screen by using *ngFor, this is Angulars handly little for loop. *ngFor loops through the employees Array and returns each employee as a separate list item filling out the list. The *ngIf will only display IF an error is received.

Now let’s look at the app.component.html now. Use this code to complete it:

<div class="content" role="main">


<fieldset style="width:90%">

<legend>Select an action</legend>


<input type="radio" id="add" name="app" [value]=1


<label for="add">Get all customers</label>




<div *ngIf="appsection==1">




<label *ngIf="error">




Here’s what we are doing in the app.component.html. Take a look at this line first:

<input type="radio" id="add" name="app" [value]=1


I’m defining the radio button as an add button and setting the appsection value to 1. When the radio button is selected the appsection==1 which we use further down to display the app-get-all-employees in this section:

<div *ngIf="appsection==1">



This is a handy little trick because we can define multiple appsections and using *ngIf display only the desired app. For example let’s say we have another app component called app-get-employee that returns a single employee. We could setup a second radio button and have it assign “2” to appsection. Then we would setup a second *ngIf for appsection==2 just like we setup appsection==1 except passing in the component like this:


In this manner we can build out an entire site as a single page app. It would be all action driven, reactive and fluid. It’s a great time to be playing on the web!

O.k. moving right along now… open up the app.module.ts and modify it to include an import and a declaration for GetAllEmployeesComponent. The import statement will look like this:

import { GetAllEmployeesComponent } from './get-all-employees/get-all-employees.component';

and you want to append this to the list of declarations in @NgModule:


Save your work and let’s move on to the app.component.ts all we need to do here is an import statement for the Employee class:

import {Employee} from ‘./employee’;

Ok now save all your work and launch the angular site. Hit that radio button, now click the Get all Employees button. BOOM a screenful of employees!

So in these last two posts I showed you how to setup a basic API call to an existing test API site. We also learned how to use appsection to load components on a page. Now go make it pretty and add more functionality.

This article is just over a year old but it has great tips and tricks for working with TypeScript and Angular.

#CS@Worcester #CS343

  • Michael Duquette

Did you do that Angular tut I provided in my last post? Pretty easy to get up and running. I did the whole Tour of Heroes tutorial over the summer so I could have a solid understanding of where I was at with it. It’s flexible and somewhat intuitive.

Ready to kick your Angular skills up a notch? Let’s create a component that does an API call that returns a list of all employee’s. We’ll code this against a working online example that’s hosted at

I’m making a couple of assumptions here. First that you actually did the earlier tutorial and second that you understand how API’s work and lastly that you have an Angular site you are currently working on. If you’re not sure about Rest API’s read this article on Sitepoint then jump back in.

Ok if you’re working with a very basic Angular site let’s open it in your favorite IDE. I like using Webstorm as it has some neat built ins. In Webstorm I’ll click in the lower toolbar and launch a terminal session in the root of my project folder.

To create the new component I’ll type in: ng g component getAllEmployees

Since you’re here go ahead and create an employee class:

ng g class employee

This will create a new Angular component named get-all-employees and will also create an employee class file in the root of your project. If you check out your projects file structure you’ll see a new folder named get-all-employees in the app folder. Browse that new folder and you’ll see that Angular created the .css .html .spce.ts and component.ts for your new component.

Now let’s get to the fun part, the coding. Open the employee.ts in the root folder and modify it like this:

export class Employee {

id: number;

employee_name: string;

employee_salary: number;

employee_age: number;

constructor(id: number, employee_name: string, employee_salary: number, employee_age: number) { = id;

this.employee_name = employee_name;

this.employee_salary = employee_salary;

this.employee_age = employee_age;



Ignore my formatting please as my blog interface makes it wonky.

By using an Employee class we can import the JSON response objects into an array of employee objects.

Save the employee.ts and open up the get-all-employees.component.ts we’ll modify this to use the employee.ts and create the methods to pass the GET request to pull in all the employees:

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

import {Employee} from '../employee';

import {HttpClient, HttpErrorResponse} from '@angular/common/http';


selector: 'app-get-all-employees',

templateUrl: './get-all-employees.component.html',

styleUrls: ['./get-all-employees.component.css']


export class GetAllEmployeesComponent implements OnInit {

employees: Employee[] = [Object];

error: HttpErrorResponse = null;

constructor(private http: HttpClient) { }

ngOnInit() {


getAllEmployees(): void {

this.error = null;

this.http.get('' )

.subscribe(response => {

this.employees = response;





Let’s break down what we are doing here. The imports should be self-explanatory but just in case…

The (Component, OnInit) are Angular core components technically since we aren’t using the ngOnInit method we really don’t need it and could remove it from this component. We import Employee so we can create an array of Employee objects. The HttpClient and HttpErrorResponse are imported because we are going to be making an http call and may have to handle an error response. The @Component section defines the get-all-employees component.

Now lets look at the class, we create the empty employees array of object type Employee.

We set a HttpErrorResponse named error to null and we call our constructor to create a private http client.

The getAllEmployees method is the where the magic happens. It takes the JSON response and maps each entry as an Employee object in the employees array.

Ok next week we will finish this up by modifying app.modules.ts, get-all-customers.component.html, app.component.html and app.component.ts

Until then – Happy Coding!

#CS@Worcester #CS343

  • Michael Duquette

So you wanna build a Single Page Web App (SPA). What’s your angle fat guy? I just wanna help man! Ok let’s take a step back here and cool off. Let’s shift our focus to building our single page web app. One of the products best suited for developing SPA’s is Angular.

What is Angular? According to this article on Sitepoint, Angular is a client-side JavaScript framework. A what? A tool that helps you develop a web application while defining how it should be designed and how it should be organized. Using Angular you’ll mainly focus on TypeScript, HTML and CSS.

Wait, I thought you said it was a JavaScript framework? It is, the compiler takes the TypeScript files you create and converts them to JavaScript for display on the web. What’s the difference? Look man this will go a lot quicker if you stop interrupting. Read this article on to see the difference between TypeScript and JavaScript.

So where do we start with our first Angular project? Let’s get our feet wet and jump in and do the Getting Started Tutorial on It’s a great tut that walks you through creating a small ecommerce web app and introduces most of the early framework principals and techniques.

Check out my next post to see some example of creating more complex SPA’s and components.

#CS@Worcester #CS343