What Is New in Angular 2.0
Angular 2.0 is a complete platform rewrite, focusing on performance, mobile support, and modern development practices. It introduces a component-based architecture, a new dependency injection system, and a streamlined change detection mechanism.
| Category | Key Changes |
|---|---|
| New Features | Component-based architecture, New template syntax, Dependency Injection, RxJS for async operations |
| Improvements | Faster change detection, Ahead-of-Time (AOT) compilation, Improved testability |
| Breaking Changes | Complete rewrite from AngularJS, New NgModule system, Simplified directives and pipes |
| Deprecated | $scope, controllerAs syntax, ng-controller, jqLite |
How does the component model change application structure?
Angular 2 shifts from the MVC controller pattern to a hierarchical component architecture. Everything is a component, which bundles its template, logic, and styles into a single cohesive unit.
This approach simplifies large-scale application development by enforcing a clear and predictable structure. Components are nestable and communicate through well-defined inputs and outputs, making code more reusable and easier to reason about.
Example Component
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `<h1>Hello {{name}}</h1>`
})
export class AppComponent {
name = 'Angular 2';
}
What is the new template syntax and data binding?
The new syntax removes many directives from AngularJS and replaces them with a simpler, more intuitive system. Property bindings use square brackets [property], event bindings use parentheses (event), and two-way binding uses the banana-in-a-box syntax [(ngModel)].
This change eliminates confusion between HTML attributes and DOM properties. In practice, it means more explicit and predictable data flow in your templates, which drastically reduces bugs related to binding context.
How does dependency injection work now?
Dependency Injection (DI) is now hierarchical and leverages ES2016 decorators. You inject dependencies directly into a component's constructor, making the dependencies explicit and the code easier to test in isolation.
The hierarchical nature means a component has access to services provided by itself and all its ancestors. This is powerful for creating application-wide singletons or scoping services to specific parts of your component tree.
Example Dependency Injection
import { Injectable } from '@angular/core';
@Injectable()
export class HeroService {
getHeroes() { return HEROES; }
}
@Component({
// ...
providers: [HeroService]
})
export class HeroListComponent {
constructor(private heroService: HeroService) { }
}
What performance improvements were made?
Angular 2 introduced a radical change detection system that is orders of magnitude faster. It uses generated code and zones to automatically know when to check for changes, moving from a slow digest cycle to a targeted, unidirectional flow.
Ahead-of-Time (AOT) compilation is a game-changer. It compiles templates during the build process, resulting in smaller bundle sizes, faster rendering, and earlier template error detection. The framework itself is also built with tree-shaking in mind from the start.
FAQ
Is Angular 2 backwards compatible with AngularJS (1.x)?
No, it is a complete rewrite. While the core concepts are similar, the APIs are different. The upgrade path involves running both frameworks side-by-side and gradually migrating components.
Why was the syntax for two-way binding changed to `[(ngModel)]`?
The new syntax makes the data flow explicit. The square brackets represent a property binding down into the component, while the parentheses represent an event binding up from the component, clearly showing the two-way communication.
What happened to $scope and controllers?
They were removed. Component classes now hold the application logic, and the template's context is the component instance itself, eliminating the need for the complex $scope hierarchy.
Do I have to use TypeScript with Angular 2?
While highly recommended and the default, you can use ES5 JavaScript. However, you'll miss out on the decorators and type safety that make the development experience much smoother.
How does the new change detection improve performance?
It bypasses the slow digest loop. By using zones, Angular is notified of async events and then checks only the components that might be affected, which is dramatically faster than checking every binding on every digest cycle.