Angular 2.0

Angular 2.0 is the version that follows AngularJS, one of the most widely used and adopted JavaScript frameworks used by millions of Web developers around the world. Angular 2.0 is only currently available as a developer preview, but it's rapidly evolving. Changes are happening so fast that some of the code parts or features described might not work after three to four months since the framework is likely to have changed by then.

Angular 2.0 applications can be developed using ES5 and ES6 (check this article for new ES6 features), but it also includes support for Dart and TypeScript. In this article, you'll see a sample application written in TypeScript.

First, you need to make sure you have installed Node.js and NPM. After these have been set up, you need to install the TypeScript compiler and the TypeScript type definitions for Angular 2.0. You can install these using the following commands:

As an example, let's create an application that displays Renaissance-era paintings and its related information. The app will look like the one displayed in the picture:

For styling, we'll be using bootstrap. Each image is displayed with its title, the painter's name, an image of the painting, and a short description. All this information will be in a TypeScript class:

The class is called PortfolioItem with four properties (these are self-explanatory) and a constructor that receives four parameters as well.

In Angular 2.0, the application is built differently compared to one that's built using Angular 1.x, which means that there's a class which is annotated. Based on these annotations, the Angular 2.0 framework will wire the logic and variables in the class to the ones defined in a template or directive.

The @Component annotation defines what HTML tag (attribute) should be mapped to this class. In this case, the selector defined for the PortfolioContainer class is portfolio, which means that here should be a tag called <portfolio> somewhere in the markup code. When the Angular 2.0 framework parses the HTML code, it maps the <portfolio> tag to this class and executes it's logic.

The @View annotation holds all the necessary information related to the HTML markup. In this case, it's the templateUrl it's loaded from and the array of angular directives that are used in that template. This sample only uses the NgFor directive, so the array only contains one item.

The class is fairly simple, as it has one member (items) under PortfolioItem arrays, a constructor, and a fetchImages method that loads up the items array with the relevant information about the images.

The last line of the app.ts file is the so called “main” method of Angular 2.0. The bootstrap method will take the class passed to it and it will build up the whole Angular 2.0 environment, bindings, and hooks using the information provided in the @Component and @View annotations.

There are two ng-for directives in the template. The first one iterates over the items array (which holds PortfolioItem arrays), while the second one creates a new thumbnail image, title, painterName and description for the PortfolioItems. The syntax of the ng-for directive in Angular 2.0 is similar to the one in Angular 1.x.

It is a normal HTML5 file, which references to bootstrap CSS files, angular2 source files and its dependencies in the <head> section. In the <body> section, there is a navigation panel defined as a classic bootstrap navigation bar. Inside the <body> tag you have the <div> with the class name portfolio and inside that you have the <portfolio> tag, which is where the template processing will happen and the HTML code generated by the Angular 2.0 framework will be inserted.

There is another important part in the page that imports the script named "app", System.import('app'). This is what loads the app.js file. The app.js file is created by the TypeScript compiler and generates normal JavaScript code. In this case, it's shown using the require.js / AMD structure to define dependencies between JavaScript modules.

Angular 2.0 is still heavily under development, but the community and the builders are trying to resolve all the issues that were discovered and reported by developers for Angular 1.x. Angular 2.0 has some major performance benefits compared to Angular 1.x, mainly in cases where you want to use other non-angular based extensions and plugins with their own binding or template engines.

I am a Software Engineer with over 7 years of experience in different domains(ERP, Financial Products and Alerting Systems). My main expertise is .NET, Java, Python and JavaScript.
I like technical writing and have good experience in creating tutorials and how to technical articles.
I am passionate about technology and I love what I do and I always intend to 100% fulfill the project which I am ...