As I mentioned in my recent blog post Angular 2 is a complete rewrite. Many concepts that are known from Angular 1.x are gone or changed dramatically. Angular 2 looks like a completely different framework: no controllers*, no DDO, no $scope, no angular.module and completely rewritten binding and change detection (no ng-model).
Although Angular 2 is still work in progress many new features have already been revealed. In this blog post I will share some basic concepts of the framework via code samples that I presented during my lecture at InfoShare 2015.
Please note the code is created with the most recent version of Angular 2 (Angular 2 Alpha 26 as of time of writing this article).
Angular 2 is built with… TypeScript (1.5)
We could say it is ECMAScript 6 on steroids, as it offers what ECMAScript 6 has to offer. But it adds many more features: type checking, interfaces, generics, enums or decorators to name just a few.
Let’s see some sample code created in TypeScript:
Let’s see two “Hello, World!” Angular 2 applications written in TypeScript and ECMAScript 5 accordingly.
As you can see the TypeScript version is a bit less verbose and decorators (@) clearly separate the code from meta data information.
Angular 2 Building Blocks
As you could notice, the the above code did not contain much of what you learned about Angular 1.x: no angular.module, no Directive Definition Object (DDO), no $scope.
A component is the main UI building block of Angular 2 app: Angular 2 is a tree of components. Every Angular 2 application must have at least one component. A component class is an execution context for the template or it is a component controller. A component requires a single @Component decorator and at least one @View decorator.
Let’s see a really basic component in Angular 2:
HelloAngular2Cmp has two properties: text and clickCount that are bound to a view defined by a template attribute of a @View decorator. Templates have access to any properties or functions of a component controller.
#variable – local variables available in the scope of the component. The variable can be later referred in the component’s template,
(event) – binding to events of element or directive,
[property] – binding to properties of element.
Every time the text property of a component changes, the change is automatically reflected in the template. The properties of elements are accessed with square brackets ([property]) syntax. In the above example the button will be disabled once the text property of a component is equal to “Angular 2”. This is a property binding in Angular 2.
To propagate changes from DOM to a component event binding must be utilized. Event bindings in Angular 2 are represented by brackets syntax: (event). In the above example when a click event occurs on a button, onClick function of a component will be executed.
To sum up, in Angular 2:
Property bindings are executed automatically, Event bindings allow propagation of DOM changes.
Property and event bindings makes two two-way data binding possiblebut an additional effort is required in order to achieve it. Fortunately, Angular 2 will be shipped with Angular 2 Forms module. This module will be used for handling user input, by defining and building a control objects and mapping them onto the DOM. Control objects can then be used to read information from the form elements.
A component is not the only UI building block in Angular 2. There are also directives which allow you to attach behavior to elements in the DOM.
The difference between a component and a directive in Angular 2 is that a component is a directive with a view whereas a directive is a decorator with no view.
A directive requires a single @Directive decorator and a directive class:
The above directive, when used on an element, adds an outline effect to the host element – the element it is used on. ElementRef is automatically provided by Angular 2 Dependency Injection module.
In Angular 2 there is a single way to inject dependencies into a directive or a component. In the below example a user-defined service is injected into a component. MessageService declares some business logic:
The component is requesting framework to provide a required dependency via its constructor:
The MessageService is imported from an external module and later it must be made available to Angular 2. One way to make dependencies available is to register them globally with a bootstrap() function like the one below:
Please note that framework services, directives or components, an iterable live list of components (@Query) and attributes (@Attribute) can be injected in the same, uniform way. The new DI module is designed the way it supports easy extending (DependencyAnnotation) so even custom dependency injection annotation can be created. In addition, Angular 2 supports injecting promises (@InjectPromise), lazy dependencies (@InjectLazy) or optional dependencies (@Optional).
As you can see Angular 2 is much different from Angular 1.x. No DDO, no $scope, no angular.module, new change detection and binding, uniform template syntax, new dependency injection, new forms module, new router and much more.
The new version of the framework should also be much simpler to learn thanks to easier and more concise concepts like component-based architecture. Moreover, Angular 2 should be significantly faster than its ancestor thanks to completely rewritten data binding and change detection.
Is there a way to migrate from Angular 1.x? In my opinion it will not be easy. But there are some tips out there that may help in migration:
Prefer one-way data-bindings to two-way data-bindings,
Use ControllerAs syntax for easier migration to components,
Do not update application state inside watch functions,
Follow the newest releases (1.4, 1.5) and upgrade,
We process cookies and make them available to Google Analytics (a service provided by Google, Inc.) to improve the performance of the website, to learn your preferences about using it and to tailor it to your needs. The data will be anonymised before being transmitted. If you do not agree to this, you may disable cookies in your browser. If you do not change your browser settings, you accept the fact that it saves cookies.