Modern web development offers an incredible variety of tools, libraries, and frameworks to build web sites/applications. As of 2016, jQuery, React, Angular, and Vue are the most popular front-end libraries/frameworks in use. They can all be used to build and accomplish similar things, but in different ways. What are the advantages/disadvantages of each? How does each approach vary? What do the new tools of React, Vue, and Angular 2 offer that Angular 1, jQuery, and vanilla JS didn’t?
In part 1 of the series, we saw the similarities and differences between javascript and jQuery while building the same pomodoro app and we also discussed the advantages and disadvantages of each. In part 2 of the series, we will see how to use angular 1 and angular 2 to build the same pomodoro timer and we will discuss the difference in approaches taken by the two frameworks.
What is a pomodoro timer?
The pomodoro timer is an easy-to-use productivity method. Using this method, you work in 25 minute spurts with 5 minute breaks in between. Many people find it helpful for helping them focus and work on tasks during the day.
Project Overview
In this tutorial, first we will create a pomodoro timer using Angular 1. Then, we will re-create the same pomodoro timer using Angular 2.
Here is how the final version of pomodoro timer built using Angular 1 looks like :
See the Pen Pomodoro Timer : : Angular1.5 by Raj Gupta (@rajdgreat007) on CodePen.
Here is how the final version of pomodoro timer built using Angular 2 looks like :
See the Pen Pomodoro Timer : : Angular2 by Raj Gupta (@rajdgreat007) on CodePen.
Our pomodoro timer shows minutes, seconds, and gives simple control buttons. It’s a simple timer but can give enormous productivity gains if used right.
Building the Pomodoro Timer : Angular 1
Let’s first write the html code to create various elements of the pomodoro timer.
Angular 1 has MVC(Model, View, Controller)/MVVM(Model, View, ViewModel) architecture. The model represents data (eg. javascript objects) of the application. The view is how we can display the data to user. Since Angular 1 supports two way data binding, any change in the model will automatically update the view and vice versa. The controller consists of the code that controls the interaction between model and view.
By setting ng-app=“pomodoroApp”, we are marking the corresponding div (with id pomodoro-app) as the root element of the Angular application. “pomodoroApp” is the name of the angular module that we will define later in javascript code. By setting ng-controller=“pomodoroController”, we are attaching a controller function (will be defined later) to the view. The controller function will consist of variables and functions needed for the view. ng-click defines the function (defined in controller) that will be executed when the corresponding button is being clicked. We will be using a background filler that will keep increasing as time progresses.
Let’s apply css to the above markup.
Now, let’s make the pomodoro app functional by adding the following javascript :
Inside the view, we are using {{toDoubleDigit(minutes)}} to display current minutes in double digits. It is bound to $scope.minutes variable in controller. Thus, as soon as the value of $scope.minutes changes, the changed value is immediately reflected in the view. Same thing happens for seconds and filler height display.
Building the Pomodoro Timer : Angular 2
Now we’re going to build this exact same app using Angular 2.
Angular 2 uses the concept of components which could be considered as Angular 1 directives that are associated with their own template. Let’s go ahead and create the pomodoro app using Angular 2 component. For Angular 2, the angular team has chosen TypeScript over JavaScript. Typescript is a superset of JavaScript and created by Microsoft. So, we will be writing code in TypeScript for the pomodoro app.
html :
Loading…
Note : The following external javascript libraries are required to be added to the page (For the demo, they are already added in copepan settings):
The Angular team has made a lot of changes in Angular 2. It would not be an anomaly to say that they completely revamped the framework and build it from scratch. The reason for doing so many changes was to remove the bad parts of Angular 1 and to take advantage of new features of ECMAScript6 (the new version of JavaScript). Let’s explore the most prominent changes made in Angular 2 and reason behind them.
Controller and scope are completely eliminated from Angular 2. Angular 2 is more focused towards Component based development and object orientation (because of typescript). Web applications nowadays have become too complex. Angular team chose to use web components in Angular 2 because other than providing encapsulation and reusability, web components manages the complexity of the application by providing isolation. Every component is independent of other components and can be used as such by simply importing it on a page. Each component has its own template and styles that will apply only to that particular component. It means that the css will not bleed out of the component. The same is not possible with Angular 1 as the CSS is applied on page level. We can even create custom elements with Angular 2 that could be reused as such or could be extended by other users.
As mentioned earlier, Angular 2 team has chosen typescript for writing code in Angular 2 . The choice is inspired from following facts :
Typescript easily integrates with most of the already existing javascript libraries and existing javascript code. So, we need not worry about legacy code.
Using typescript adds lot of advantages to the framework which includes optional static typing and support for object oriented features like inheritance and interfaces. Static typing allows the type checking during the compilation. An interface define a set of functionality as a contract. When the interface is implemented, all the members of the interface must be implemented. Traditional javascript support prototype-based inheritance which is not completely object oriented. Typescript supports object oriented class based inheritance which goes pretty well with other object oriented features.
Typescript adds type support to javascript. It means that the bugs that are caused by false assumption of a variable being of certain type can be detected while compilation. The same is not possible with javascript as it is dynamically typed.
Typescript is far better than core javascript in managing the complexity of large projects.
Angular 1 implements two-way data binding by dirty-checking method and use watchers to watch the model/view for changes. Higher number of data bindings means higher number of watchers and hence higher performance degradation. Angular 2 doesn’t come with built in two-way data binding, although even in Angular 2, we can create directives that support two-way data binding. Angular 2 primarily uses unidirectional tree based change detection which boosts performance. Although we are not using two way data binding in Angular 1 pomodoro app, its worth mentioning that it is considered to be one of the bad parts of Angular 1 and thus it is removed from Angular 2.
Because of the optimisations mentioned above, Angular 2 is considered to be 5 times faster as compared to Angular 1 (as per ng-conf [https://ti.to/ng-conf/2016/en]).
In Angular 1, the bootstrapping of the app could be done by using ng-app or by calling the bootstrap function. In Angular 2, it can only be done by calling the bootstrap method.
Angular 1 uses $interval which is Angular’s wrapper for window.setIntervalfunction. As specified in code, the callback function is executed after every 1000 ms. Intervals created by $interval service must be explicitly destroyed because they are not destroyed automatically after the controller scope is destroyed. $interval is not available in Angular 2, thus we can either use javascript’s setInterval orObservable.interval method of Angular 2.
Conclusion
We created two pomodoro apps, one using Angular 1 and other using Angular 2. The app built using Angular 2 carries the advantages of web components and shadow DOM and is also free from the performance degradation caused due to two-way data binding of Angular 1.
Bonus
Here is this pomodoro app done in React
See the Pen Pomodoro Timer : React by Raj Gupta (@rajdgreat007) on CodePen.
Here is this pomodoro app done in Vue
See the Pen Pomodoro Timer : : Vue by Raj Gupta (@rajdgreat007) on CodePen.