Angular - A Primer
When Sir Timothy Berners-Lee invented the Internet, he never anticipated that the Internet would be used to publish selfies, share cat videos, or bomb web page with ads. His main intention (guessing) was to create a web of documents so a user on the Internet can access these hypertexts from anywhere and make use of it.
An interesting article published by Craig Buckler at Sitepoint titled, The Web Runs Out of Disk Space (http://www.sitepoint.com/web-runs-disk-space/), shows how the content on the Internet is spread out:
- 28.65% pictures of cats
- 16.80% vain selfies
- 14.82% pointless social media chatter
- 12.73% inane vlogger videos
- 9.76% advertising/clickbait pages
- 8.70% scams and cons
- 4.79% articles soliciting spurious statistics
- 3.79% new JavaScript tools/libraries
- 0.76% documents for the betterment of human knowledge
You can see, since the invention of the Internet to the present day, how we have evolved. Better evolution needs better frameworks to build and manage such apps that need to be scalable, maintainable, and testable. This is where Angular stepped in back in 2010 to fill the gap and it has been evolving quite well since then.
We are going to start our journey by understanding the new changes to Angular, the importance of TypeScript, and see how Ionic 2 has adapted itself with Angular to help build performance-efficient and modern Mobile Hybrid apps.
In this chapter, we will take a quick peek at new topics added as part of Angular with the help of an example. The main changes that have taken place in Angular (2) are primarily on the lines of performance and componentization, apart from the language update. We will be going through the following topics in this chapter:
- What is new in Angular?
- TypeScript and Angular
- Building a Giphy app
What is new in Angular?
Angular 2 is one of the most anticipated and dramatic version upgrades I have seen for any software. Angular 1 was a boon to web/mobile web/hybrid app developers, where managing a lot of things was made easy. Not only did Angular 1 help restructure client-side app development, but it also provided a platform to build applications; not websites, but applications. Though the first release suffered performance issues when dealing with large datasets, the Angular team bounced back quite well with the later releases of Angular 1, that is, Angular 1.4.x and above, and fixed these performance issues by releasing a more stable version in the form of Angular (2).
Some of the new changes that have accompanied with Angular (2) are:
- Speed and performance improvements.
- Component based (not the typical MV*).
- Angular CLI.
- Simple and expressive syntax.
- Progressive Web Apps (PWA).
- Cross-platform app development, which includes desktops, mobile, and web.
- Cordova-based Hybrid app development.
- Angular Universal provider for the server side for fast initial views.
- Upgrades to better animation, internationalization, and accessibility.
- Angular can be written on ES5, ES6, TypeScript, and Dart are based on the user's comfort with the JavaScript flavor.
With these new updates, developing apps has never been easier, be it on the desktop, mobile, or Mobile Hybrid environments.
Note: The latest version of Angular is going to be called just Angular, not Angular 2, or AngularJS 4, or NG4. So throughout this book, I will refer to Angular version 2 as Angular.
The current latest version of Angular is 4. Do checkout Chapter 11, Ionic 3, to know a bit more about Angular 4 and how it improves Ionic.
Note: If you are new to Angular, you can refer to these books:
https://www.packtpub.com/web-development/learning-angular-2
https://www.packtpub.com/web-development/mastering-angular-2-components
https://www.packtpub.com/web-development/mastering-angular-2
https://www.packtpub.com/web-development/angular-2-example
Or these videos:
https://www.packtpub.com/web-development/angular-2-projects-video
https://www.packtpub.com/web-development/web-development-angular-2-and-bootstrap-video
https://www.packtpub.com/web-development/angular-2-web-development-TypeScript-video
TypeScript primer
Angular uses TypeScript extensively for app development. Hence as part of the Angular primer, we will refresh the necessary TypeScript concepts as well.
If you are new to TypeScript, TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. TypeScript provides static typing, classes, and interfaces and supports almost all features of ES6 and ES7 before they land in the browser.
A TypeScript file is saved with a .ts extension.
The main advantage of adding typings to an untyped language (JavaScript) is to make IDEs understand what we are trying to do and better assist us while coding; in other words, Intellisense.
Having said that, here is what we can do with TypeScript.
Variable typing
In vanilla JavaScript, we would do something like this:
But in TypeScript, we cannot do as shown in the preceding code snippet, the TypeScript compiler would complain as we are modifying the variable type at runtime.
Defining types
When we declare variables, we can optionally declare the types of variables. For instance:
This increases the predictability of what we are trying to do.
Classes
I am a guy who believes that JavaScript is an object-based programming language and not an object-oriented programming language, and I know quite a lot of people who disagree with me.
In vanilla JavaScript, we have functions, which act like a class and exhibit prototype-based inheritance. In TypeScript/ES6, we have the class construct:
In the preceding example, we have defined a class named Person and we are defining the class constructor, which accepts the name on initialization of the class.
To initialize the class, we will invoke the class with a new keyword and pass in the name to the constructor. The variable that stores the instance of the class -- the object, arvind in the preceding example, can also be typed to the class. This helps in better understanding the possibilities of the arvind object.
Note: The classes in ES6 still follow Prototypal-based Inheritance and not the classical Inheritance model.
Interface
As we start building complex apps, there will be a common need for a certain type of structure to be repeated throughout the app, which follows certain rules. This is where an interface comes into the picture. Interfaces provide structural subtyping or duck typing to check the type and shape of entities.
For instance, if we are working with an app that deals with cars, every car will have a certain common structure that needs to be adhered to when used within the app. Hence we create an interface named ICar. Any class working with cars will implement this interface as follows:
Modules and imports
In vanilla JavaScript, you must have observed code blocks like this:
Modules are achieved in ES6/TS using the imports and exports syntax:
These are the bare essentials that we would need to get started with TypeScript. We will look at more such concepts where needed.
With this we wrap up the key concepts needed to get started with TypeScript. Let us get started with Angular.