Angular 1.x Vs Angular 2.0

Introduction

Angular is a platform to develop Web and mobile applications. Angular 2 is not just an update of Angular 1.x but Angular 2.0 is re-written and has many breaking changes. It is completely written in typed script (to meet ES 6 specifications). There will be a huge learning curve for the developers of Angular 2.

Difference between Angular 1.x and Angular 2.0



No Controller and $Scope

Angular 1.x is required to create a Controller and $scope. Both no longer exist in Angular 2.0. Angular 1.x’s Controller is replaced with the component in Angular 2.0.

Angular 1.x

  1. varmyApp = angular.module("myApp", [])  
  2. .controller("empController", function($scope) {  
  3.   
  4.    $scope.name = "Jignesh Trivedi";  
  5. });  
Angular 2.0
  1. import { Component } from 'angular2/core';  
  2.   
  3. @Component({  
  4.   selector: 'mydata',  
  5.   template: `  
  6. <h3>{{name}}</h3> `  
  7. })  
  8.   
  9. export class ProductComponent {  
  10.   name = "Jignesh Trivedi;  
  11. }  
Angular 2.0 has introduced a new keyword, @Component, that is used to add metadata to the class.

Way of Bootstraping Angular Application is changed

Angular 1.x has two ways to bootstrap Angular. 
  • Using directive ng-app.
  • Using Angular.bootstrap function.

Angular 2.0 has only one way to bootstrap Angular Application, using the bootstrap function.

  1. import { bootstrap } from 'angular2/platform/browser';  
  2. import { TestComponent } from './Test.component';  
  3.   
  4. bootstrap(TestComponent);  
Structural directives syntax is changed

ng-repeat directive is now replaced by *ngFor. ng-If is replaced with *ngIF. Angular 2.0.  Camel Case syntax and Asterisk (*) sign is used as a prefix for the structural directives.

Angular 1.x
  1. <ul>  
  2.    <li ng-repeat="name in memberNames">  
  3.       {{name}}  
  4.    </li>  
  5. </ul>  
  6. <div ng-if="memberNames.length">  
  7.    <h3>You have {{memberNames.length}} Members.</h3>  
  8. </div>  
Angular 2.0
  1. <ul>  
  2.    <li *ngFor="let name of memberNames">  
  3.       {{name}}  
  4.    </li>  
  5. </ul>  
  6. <div *ngIf="memberNames.length">  
  7.    <h3>You have {{memberNames.length}} Members.</h3>  
  8. </div>  
Local variables are defined, using let keyword.

Built-in directives syntax is changed

Angular 2.0 uses Camel case syntax for built-in directives. For example, ng-class is replaced with ngClass, ng-model is replaced with ngModel etc.

Event Syntax is changed

Angular 2.0 directly uses the valid HTML DOM element events. For example, ng-click is now replaced with (click)

Angular 1.x
  1. <button ng-click="clickEvent()">  
Angular 2.0
  1. <button (click)="clickEvent()">  
Some build-in directive no longer exist

Angular 2.0 directly uses the valid HTML DOM element properties. Due to these changes, many built-in directives of Angular 1.x are now no longer required. For example, ng-href, ng-src, ng-show and ng-hide is no longer required. Angular 2.0 use href, src and hidden instead of ng-href, ng-src, ng-show and ng-hide directive.

One way data binding directive replaced with [property]

ng-bind directive is used for one way binding in Angular 1.x. Angular 2.0 uses HTML DOM element property for one way binding.

Angular 1.x
  1. <input type="text" ng-bind="name"></input>  
Angular 2.0
  1. <input type="text" [value]="name"></input>  
Note that the square brackets are used with property name for one way data binding.

Two way data binding: ng-model replaced with [(ngModel)]

ng-model directive is used for two way data binding in Angular 1.x, but it is replaced with [(ngModel)] in Angular 2.0.

Angular 1.x
  1. <input type="text" ng-model="name"></input>  
Angular 2.0
  1. <input type="text" [(ngModel)] ="name"></input>  
Way of defining Service is changed

There are five different ways to define the Service in Angular 1.x. 
  • Service
  • Factory
  • Constant
  • Provider
  • Values

Angular 2.0 has only one way to define the Service.

  1. import { Injectable } from 'angular2/core';  
  2. @Injectable()  
  3. export class DataService {  
  4.     constructor(public http: Http) {  
  5. this.http = http;  
  6.     }  
  7. getTestData() {  
  8.         return this.http.get("/api/Home/GetTestData")  
  9.             .map((responseData) =>responseData.json());  
  10.     }  
  11. }  
Here @Injectable() is added to the Service class, which is similar to $inject used in Angular 1.x, both are used for Dependency Injection.

Way of routing is Changed- syntax changed

$routeProvider is used to configure routing and ng-view is used to load the view in Angular 1.x. In Angular 2, these are replaced with @RouteConfig and <router-outlet>respectively.

Angular 1.x
  1. var app = angular  
  2.         .module("MyModule", ["ngRoute"])  
  3.         .config(function ($routeProvider) {   
  4.             $routeProvider  
  5.               .when("/home", { templateUrl: "home.html", controller: "homeController" })  
  6.   
  7.         })  
  8.        .controller("homeController", function ($scope) {  
  9.             $scope.message = "This is Home Page";   
  10.         })  
In HTML
  1. <ul>  
  2.    <li><a ng-href="#page4">Home</a></li>  
  3. </ul>  
Angular 2.0 
  1. import { Component } from 'angular2/core';  
  2. import { RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from 'angular2/router';  
  3.   
  4. @Component({  
  5.   selector: 'my-app',  
  6. templateUrl: 'app/app.component.html',  
  7.   directives: [ROUTER_DIRECTIVES],  
  8.   providers: [  
  9.     ROUTER_PROVIDERS  
  10.   ]  
  11. })  
  12. @RouteConfig([  
  13.   { path: '/home', name: 'Home', component: HomeComponent, useAsDefault: true },  
  14. ])  
  15. export class AppComponent { }  
In HTML
  1. <ul>  
  2.    <li><a [routerLink]="['Home']" href="">Home</a></li>  
  3. </ul>  
Angular 2.0 has built-in support for mobile applications whereas Angular 1.x did not have supports for mobile.

Angular 2.0 supports TypeScript as development language whereas Angular 1.x is not supporting TypeScript.

Summary

Angular 2.0 is completely different from Angular 1.x. It requires some effort to migrate from Angular 1.x to Angular 2.0. Angular 2.0 implements web standards, for example, component, and it provides better performance than Angular 1.x. 

Up Next
    Ebook Download
    View all
    Learn
    View all