Introduction

Hi there, nice to see you here. In this Angular series, we’ll explore everything about Angular. We’ll make an e-commerce application using what we’ll learn in this series. So, let’s get started with Angular.

Angular is nothing but a framework for building client applications in HTML, CSS and Javascript/Typescript. Typescript compiles into Javascript. Typescript is more common in the Angular community because Angular is written itself in Typescript. If you never worked with Typescript before, don’t worry, we’ll explore it in this series.

Roadmap

This is the first article of our Angular Roadmap. Remaining articles are here

Why Angular when we already have Javascript/jQuery?

Can we not use plain old Javascript or jQuery? We certainly can, and there are a lot of web applications already working with Javascript and jQuery. As our application gets more complex, our Javascript and jQuery code is hard to maintain. We need a way to properly structure our application. Well, Javascript Patterns {Module Patterns, Prototype Patterns} help us for structuring, but these patterns are hard to understand for the beginners to Javascript. We already know that Javascript and jQuery are hard to test so for a couple of years many frameworks have been built and evolved to make web application development easier. Angular is an example of such a framework.

It gives our application a clean and loose structure that is easy to understand and easy to maintain. It also brings a lot of utility code that we can reuse in various applications especially when dealing with users' navigation and the browser history. Applications developed by Angular are more testable so we can easily write automated test (unit test) to test various parts of our application.

So, the final answer is Angular is not essential but it makes your life a lot easier.

The architecture of Angular Applications

A lot of modern application have at least 2 parts

How Do We Save Our Data In Angular?

We don't save the data in Client side because it can easily disappear as the user clears the browser data or moves to a different computer, that's why we store the data into the server. So, here we often have one or more databases as well as a bunch of HTTP services or APIs to make this data available to the client.

These HTTP services/APIs are the endpoints that are accessible via the HTTP Protocol. So we call the simple HTTP to get or to save the data.

Angular

Now, let’s make our environment ready for Angular.

Step 1

Step 2

Now, we’ll install Angular CLI through npm. Angular CLI is a command line tool that we used to create a new Angular project or generate some boilerplate code (scaffolding) as well as deploy the packages on the server. So, let’s install Angular cli. The command pattern to install anything in the system with npm is.

  1. npm install -g PackageName

-g means for global installation and if you don’t put (-g) here, Angular CLI will be installed only in the current folder and it will not be accessible anywhere else.

Run the command prompt as (Run as Administrator)

  1. C:\Windows\system32> npm install -g @Angular/cli

After completing the process we need to make sure whether the package is installed or not. So, run the command.

  1. C:\Windows\system32> ng --version
Angular

Step 3

Now, if you don’t have Visual Studio Code installed then install it, please. And if you’ve installed it then open the Visual Studio code.

And open the integrated terminal.

Angular

As we already know, one solution or one directory can have multiple projects. So, instead of making the project directly, let’s create a directory and then make the Angular project in that directory through the terminal.

  1. PS C:\Users\Ami Jan> mkdir HelloWorld
  2. PS C:\Users\Ami Jan> cd HelloWorld
  3. PS C:\Users\Ami Jan\HelloWorld> ng new MyFirstAngularProject
Angular

During the project creation, you might see this error

Angular

In this scenario you just need to run these commands one by one, actually, we need here GitHub email address and username.

  1. PS C:\Users\Ami Jan\HelloWorld> git config --global user.email [email protected]
  2. PS C:\Users\Ami Jan\HelloWorld> git config --global user.name Usama

And then finally your project will be created when you again try to create the Angular project.

Angular

I’m still facing some warnings here, we should not be bothered about these warnings. These file flags are actually used by IDE under the hood. We can build and run our application without any issue.

  1. PS C:\Users\Ami Jan\HelloWorld> code .

It will open our project in Visual Studio code editor.

Angular

Now, we need to make sure whether it is running or not on the server. So, come back to the Visual Studio code terminal and make sure the directory folder is first.

  1. PM > cd .\MyFirstAngularProject\
  2. PM > ng serve
Angular

As we can see our project is compiled successfully with localhost:4200 and Angular has created the js and CSS files for us.

Step 4

Now, open the localhost link in the browser.

Angular

Angular Project Demonstration

Webpack

Now let’s make some changes here in this application and let me show you the magic of Angular.

AngularCLI uses the tool web pack which is actually the automation tool, it gets all our stylesheets and javascript and combines them in a bundle and then minifies that bundle for optimization purposes. Web pack runs whenever we save any changes in the project and updates the data on the server without refreshing the application. This feature of web pack is called Hot Module Replacement (HMR).

Now, if we open our browser and inspect the page, then we’ll see the references on the page.

Angular

And if we open our index.html page from “src” folder, we have no file reference in the file.

Angular

These references are dynamically added by web pack into the HTML file.

Notice

If you see the page view source, we have styles.js which means all our stylesheets are compiled into the JavaScript bundle. All our styles work inside the JavaScript.

The difference of Angular Version History

AngularJS was first introduced in 2010 as a JS Framework for building a client application. Soon it gained popularity. And Angular team started adding new features to the core. But the framework was not designed with the needs of today’s applications in mind and it was overly complex. So Angular team decided to rewrite the original framework using typescript.

And as a result, Angular 2 came out in mid-2016. This new version is an entirely different version from Angular 1. And you can feel that it is an entirely different thing. This made a lot of developers unhappy because they had a lot of applications built with Angular 1 and with each application over a few thousand lines of code had to be rewritten. But this new framework is a much cleaner framework and it is a lot easier to understand and to work with.

And after a few minor upgrades to Angular 2, something strange happened eventually.

2.0 -> 2.1 -> 2.2 -> 2.3 -> 4 ( suddenly Angular 4 came out)

This made developers wonder what happen to Angular 3. But unlike Angular 2, Angular4 was not a new framework with a lot of breaking changes. In fact, it wasn't even a major upgrade.

Let's see what happens.

Angular consists of few libraries that are distributed as a separate node package via npm i.e.

@angular/code 2.3.0

@angular/compiler 2.3.0

@angular/HTTP 2.3.0

@angular/router 3.3.0

and a few other libraries. All these libraries were the same version except the router library. So in order to align these versions and avoid confusion in the future, the Angular team decided to create Angular version 4.

So Angular 4 is not the major upgrade to Angular 2 and you can even think of it as Angular 2.4 simply.

After all these confusions in the community about Angular versions, the team decided to drop the version suffix and simply call the framework Angular.

So now, we have 2 kinds of Angular

AngularJS (1.x) 1st gen of Angular, written JS and it is going to die sooner or later

Angular (2+)

Conclusion

We’ve learned a lot about Angular here. We’ve created the Angular 6 application using Visual Studio Code Terminal and with the help of Angular cli. We’ve also displayed the custom message on the screen and we’ve explored web pack and different versions of Angular. See you in the next part of the series.