Introduction
@H_
502_0@Modern application development has gone through dramatic changes in the past few years with frameworks like Angular,ReactJs and others. With ES6 changes,Typescript and Angular2 we are seeing a new wave of changes coming to the web application development. This tutorial walks through a process of creating a basic Angular2 application using Visual Stu
dio.
@H_
502_0@
Background
@H_
502_0@This tutorial tries to set up an environment to build Angular2 apps using Visual Stu
dio 2013 and in the process describes how different pieces come together for an Angular2 app . The tools and technologies used in this tutorial are
@H_
502_0@1) Visual Stu
dio 2013 Community Edition with .NET 4.5
@H_
502_0@2) Angular 2
@H_
502_0@3) Typescript
@H_
502_0@4) NPM
@H_
502_0@Let's get started!
Installing TypeScript
@H_
502_0@This tutorial needs typescript 1.6.2 or later to be installed with your Visual Stu
dio . You can download typescript for Visual Stu
dio 2013 from here
@H_
502_0@
https://www.microsoft.com/en-us/download/details.aspx?id=48739
@H_
502_0@Once installed you can see the all versions installed at following location on your machine at the following location.
@H_
502_0@{Drive}\{Program Files Folder}\Microsoft SDKs\TypeScript\
@H_
502_0@You can verify the typescript version by typing the following command on command prompt.
@H_
502_0@
https://www.codeproject.com/KB/scripting/1060262/TscVersion.png
@H_
502_0@If you still do not see the correct version check the PATH environment variable to make sure it is pointing to the location of the latest version on your machine.
@H_
502_0@@L_
403_6@
Installing Node.js/npm
@H_
502_0@npm (Node Package Manager) is
required for installing Angular2 . npm is installed as part of Node.js installation. You can download Node.js installer from the
Node.JS site. Once installed you can type the following command to check the node js version installed.
npm -v
@H_
502_0@
https://www.codeproject.com/KB/scripting/1060262/NodeInstallation.png
Setting Up Visual Studio Project
@H_
502_0@1) Launch you Visual Stu
dio and create a new project. Select the HTML with TypeScript template.
@H_
502_0@
https://www.codeproject.com/KB/scripting/1060262/ProjectTemplate.png
@H_
502_0@2) The project will have index.html,app.ts and app.cs included in the project.
@H_
502_0@
https://www.codeproject.com/KB/scripting/1060262/ProjectStructure.png
@H_
502_0@3) Under project properties update the TypeScript Build options to include module system. Angular2 exposes the API as modules so we will need a module handler to manage modules. We will be using systemjs for module loading. We will be installing it in next step.
@H_
502_0@
https://www.codeproject.com/KB/scripting/1060262/ProjectProperties.png
@H_
502_0@To avoid compilation errors related to Decorators (see below) we will have to turn on the
experimentalDecoratorsflag for TypeScript compiler. You will have to modify the .csproj file manually to include the flag. Close your Visual Stu
dio solution and open the .csproj file any text editor of your choice and add the following element to project properties.
<TypeScriptExperimentalDecorators>true@H_502_154@</TypeScriptExperimentalDecorators>
@H_
502_0@Here is a screenshot of the cs proj file
@H_
502_0@
https://www.codeproject.com/KB/scripting/1060262/ExperimentalDeciaratorspng.png
@H_
502_0@without this you might see the following errors in you TypeScript files.
@H_
502_0@
https://www.codeproject.com/KB/scripting/1060262/BuildError.png
Adding Angular2 & SystemJS Modules.
@H_
502_0@To install Angular2 navigate to you project folder from command prompt and install the following npm packages.
- angular2 - Angular 2 library.
- systemjs - an opensource library for module loading.
npm install angular2 systemjs --save --save-exact
@H_
502_0@If you click on
Show All Filesin Visual Stu
dio solution explorer,
node_modulesfolder should be visible with folders for the angular2 and systemjs.
@H_
502_0@
https://www.codeproject.com/KB/scripting/1060262/Dependencies.png
@H_
502_0@With all necessary dependencies installed let start writing our application.
The Application Component
@H_
502_0@We will be creating a simple contact manager app which displays the contact name,email address and contact number. Modify the contents of app.ts file to the listing below.
@H_502_154@import {Component,View,bootstrap} from "angular2/angular2";
@Component(
{
selector: 'contact'
})
@View({
templateUrl: Contact.html'
})
@H_502_154@class Contact {
name: string;
email: string;
phone: string;
@H_502_154@constructor() {
@H_502_154@this.email = John.Doe@gmail.com';
@H_502_154@this.name = John Doe';
@H_502_154@this.phone = 1-800-GOOG';
}
}
bootstrap(Contact);
@H_
502_0@Add a html file to your project and name it Contact.html. Modify the contents as below.
@H_502_154@<div class@H_502_154@="@H_502_154@container"@H_502_154@>
div@H_502_154@>
label for@H_502_154@name"@H_502_154@>Name:/labelspan@H_502_154@>{{name}}/span/div@H_502_154@>{{email}}@H_502_154@>{{phone}}@H_502_154@>
@H_502_154@>
@H_
502_0@This is a very simple component but let us look at some interesting things going on here.
TypeScript Definition Files
@H_
502_0@The first two lines of the app.ts are references to typed definition files. Typed Definition Files expose the public API of the a library (in this case Angular2) and are used by the compiler to resolve references for external modules. In our case we are using the @Component,@View and bootstrap modules declared in angular2.d.ts file.
Importing Modules
@H_
502_0@In the next line we are importing external modules
required by our component. In Angular2 you are
required to import any dependencies you need to build your component and not included by default. If you look at the
angular2.d.tsfile located in node_modules\angular2\bundles\typings\angular2folder,you will notice that Component,View and Bootstrap are exported externally as angular2/angular2 module.
@H_
502_0@A basic Angular2 component consists of 3 parts
@H_
502_0@1) Component annotation
@H_
502_0@2) View Annotation
@H_
502_0@3) Classes
Classes
@H_
502_0@The Contact class defined is essentially our controller with defined properties. It has been defined using TypeScript and had three defined properties of name,email and phone. The constructor of the class initialized the name,email and phone properties which will be populated on our view.
Annotation/Decorators
@H_
502_0@The annotations are ways of adding
Meta-data to the class and will be used by Angular to find and load our component into the DOM . In this case @Component defines a selector (<contact></contact>) and replaces it by the View defined in @View annotation.
View & Databinding
@H_
502_0@The view defined in our component is Contact.html. If you notice the listing for contact.html you will notice that the all the curly braces{{ }} are used for data-bindings to the component properties. The data-binding expressions will resolve to the class properties.
Bootstrapping
@H_
502_0@The final thing is to bootstrap our application. Angular will now know which component to use and will load the component into the element that matches our selector in DOM.
HTML File
@H_
502_0@Modify the contents of index.html file as follows.
!DOCTYPE htmlhtml lang@H_502_154@en"@H_502_154@>
headMeta charset@H_502_154@utf-8" @H_502_154@/title@H_502_154@>First Angular App/titlelink rel@H_502_154@stylesheet" href@H_502_154@app.css" type@H_502_154@text/css" @H_502_154@>
script src@H_502_154@node_modules/systemjs/dist/system.js"@H_502_154@></script>
@H_502_154@node_modules/angular2/bundles/angular2.js"@H_502_154@app.js"@H_502_154@></script>
/headbodyscript@H_502_154@>
System.import(
</script>
h1@H_502_154@>TypeScript HTML App/h1@H_502_154@>
id@H_502_154@content"contact@H_502_154@>/contact/body/html@H_502_154@>
@H_
502_0@Few points to notice here
- If you notice here we are referencing the js files and not the .ts TypeScript files. The TypeScript files are required when compiling the project but we need references to the js files in our HTML.
- The TypeScript compiler will generate an app.js file from the app.ts typescript file referenced on our page.
- In addition we are referencing the angular2.js and system.js files in our page.
- We are calling the System.import method to register our angular component. During the registration process Angular2 will bootstrap the application by loading the component into the specified selector.
Running Application
@H_
502_0@Now when you run the application you should see the following in your browser
@H_
502_0@.
https://www.codeproject.com/KB/scripting/1060262/Screenshot.png
History
@H_
502_0@Initial Draft- 11/29/2015