Getting Started With Angular 2 Application Using Visual Studio 2015

There are many IDEs available in the market for developing Angular 2 applications, like Sublime Text, Visual Studio Code... etc.

This article explains how to develop Angular 2 applications, using Visual Studio 2015. If you want to use Visual Studio Code IDE to develop Angular 2 applications, I recommend you follow the below link.

Getting started with Angular 2 application using Visual Studio Code

Before getting started to develop Angular 2 app, please install the following software.

  1. Prerequisites




    Select Online -> Search for “typescript” -> Download latest version of Typescript for Visual Studio 2015.



    Install Typescript for Visual Studio 2015 after downloading.

  2. Create New Project

    Open Visual Studio and then create new project.



    Then, select ASP.NET web application from Visual C# -> Web template. Give project name and click OK. 

    Select "Empty" from the available templates and leave the things as they are and click OK to create the project.



    Once the project is created, then it will form the project structure, as shown in the below image.



  3. Create Configuration files

    • package.json

      Right click on the solution and add new JSON file. Name it as package.json.



      Add the following code in package.json file.
      1. {  
      2.   "name""angular2-demo-visual-studio",  
      3.   "version""1.0.0",  
      4.   "description""QuickStart package.json from the documentation, supplemented with testing support",  
      5.   "scripts": {  
      6.     "start""tsc && concurrently \"tsc -w\" \"lite-server\" ",  
      7.     "docker-build""docker build -t ng2-quickstart .",  
      8.     "docker""npm run docker-build && docker run -it --rm -p 3000:3000 -p 3001:3001 ng2-quickstart",  
      9.     "pree2e""npm run webdriver:update",  
      10.     "e2e""tsc && concurrently \"http-server -s\" \"protractor protractor.config.js\" --kill-others --success first",  
      11.     "lint""tslint ./app/**/*.ts -t verbose",  
      12.     "lite""lite-server",  
      13.     "postinstall""typings install",  
      14.     "test""tsc && concurrently \"tsc -w\" \"karma start karma.conf.js\"",  
      15.     "test-once""tsc && karma start karma.conf.js --single-run",  
      16.     "tsc""tsc",  
      17.     "tsc:w""tsc -w",  
      18.     "typings""typings",  
      19.     "webdriver:update""webdriver-manager update"  
      20.   },  
      21.   "keywords": [],  
      22.   "author""",  
      23.   "license""ISC",  
      24.   "dependencies": {  
      25.     "@angular/common""~2.0.1",  
      26.     "@angular/compiler""~2.0.1",  
      27.     "@angular/core""~2.0.1",  
      28.     "@angular/forms""~2.0.1",  
      29.     "@angular/http""~2.0.1",  
      30.     "@angular/platform-browser""~2.0.1",  
      31.     "@angular/platform-browser-dynamic""~2.0.1",  
      32.     "@angular/router""~3.0.1",  
      33.     "@angular/upgrade""~2.0.1",  
      34.   
      35.     "angular-in-memory-web-api""~0.1.1",  
      36.     "bootstrap""^3.3.7",  
      37.     "systemjs""0.19.39",  
      38.     "core-js""^2.4.1",  
      39.     "reflect-metadata""^0.1.8",  
      40.     "rxjs""5.0.0-beta.12",  
      41.     "zone.js""^0.6.25"  
      42.   },  
      43.   "devDependencies": {  
      44.     "concurrently""^3.0.0",  
      45.     "lite-server""^2.2.2",  
      46.     "typescript""^2.0.3",  
      47.     "typings""^1.4.0",  
      48.   
      49.     "canonical-path""0.0.2",  
      50.     "http-server""^0.9.0",  
      51.     "tslint""^3.15.1",  
      52.     "lodash""^4.16.2",  
      53.     "jasmine-core""~2.5.2",  
      54.     "karma""^1.3.0",  
      55.     "karma-chrome-launcher""^2.0.0",  
      56.     "karma-cli""^1.0.1",  
      57.     "karma-htmlfile-reporter""^0.3.4",  
      58.     "karma-jasmine""^1.0.2",  
      59.     "karma-jasmine-html-reporter""^0.2.2",  
      60.     "protractor""^3.3.0",  
      61.     "rimraf""^2.5.4"  
      62.   },  
      63.   "repository": {}  
      64. }  
      The package.json file contains the dependency packages to be installed in order to work with Angular 2 applications.

    • tsconfig.json

      Create new JSON file and name it as tsconfig.json file, tsconfig file contains code that is used to convert typescript files into javascript.

      Add the following code.
      1. {  
      2.   "compilerOptions": {  
      3.     "target""es5",  
      4.     "module""commonjs",  
      5.     "moduleResolution""node",  
      6.     "sourceMap"true,  
      7.     "emitDecoratorMetadata"true,  
      8.     "experimentalDecorators"true,  
      9.     "removeComments"false,  
      10.     "noImplicitAny"false  
      11.   }  
      12. }  


    • typings.json

      Create new JSON file and name it as typings.json file and add the following code.
      1. {  
      2.   "globalDependencies": {  
      3.     "angular-protractor""registry:dt/angular-protractor#1.5.0+20160425143459",  
      4.     "core-js""registry:dt/core-js#0.0.0+20160725163759",  
      5.     "jasmine""registry:dt/jasmine#2.2.0+20160621224255",  
      6.     "node""registry:dt/node#6.0.0+20160831021119",  
      7.     "selenium-webdriver""registry:dt/selenium-webdriver#2.44.0+20160317120654"  
      8.   }  
      9. }  
    • systemjs.config.js

      Create a JavaScript file and name it as systemjs.confg.js and add the following code.
      1. /** 
      2.  * System configuration for Angular samples  
      3.  **/  
      4. (function (global) {  
      5.   System.config({  
      6.     paths: {  
      7.       // paths serve as alias  
      8.       'npm:''node_modules/'  
      9.     },  
      10.     // map tells the System loader where to look for things  
      11.     map: {  
      12.       // our app is within the app folder  
      13.       app: 'app',  
      14.   
      15.       // angular bundles  
      16.       '@angular/core''npm:@angular/core/bundles/core.umd.js',  
      17.       '@angular/common''npm:@angular/common/bundles/common.umd.js',  
      18.       '@angular/compiler''npm:@angular/compiler/bundles/compiler.umd.js',  
      19.       '@angular/platform-browser''npm:@angular/platform-browser/bundles/platform-browser.umd.js',  
      20.       '@angular/platform-browser-dynamic''npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',  
      21.       '@angular/http''npm:@angular/http/bundles/http.umd.js',  
      22.       '@angular/router''npm:@angular/router/bundles/router.umd.js',  
      23.       '@angular/forms''npm:@angular/forms/bundles/forms.umd.js',  
      24.       '@angular/upgrade''npm:@angular/upgrade/bundles/upgrade.umd.js',  
      25.   
      26.       // other libraries  
      27.       'rxjs':                      'npm:rxjs',  
      28.       'angular-in-memory-web-api''npm:angular-in-memory-web-api',  
      29.     },  
      30.     // packages tells the System loader how to load when no filename and/or no extension  
      31.     packages: {  
      32.       app: {  
      33.         main: './main.js',  
      34.         defaultExtension: 'js'  
      35.       },  
      36.       rxjs: {  
      37.         defaultExtension: 'js'  
      38.       },  
      39.       'angular-in-memory-web-api': {  
      40.         main: './index.js',  
      41.         defaultExtension: 'js'  
      42.       }  
      43.     }  
      44.   });  
      45. })(this);  
    Systemjs.config.js is used to load the angular bundles by mapping it to the JavaScript files (compiled by typescript compiler) .

    It also tells where to look for the app files like components, modules and main typescript files.

  4. Create app folder

    Right click on the solution and add new folder and name it as “app”. This folder will hold the typescript file like module, component and main page.


    • app.module.ts

      Create new typescript file and name it app.module.ts. Once the file is created It will ask for confirmation, select No.



      Copy the following code in app.module.ts file.
      1. import { NgModule } from '@angular/core';  
      2. import { BrowserModule } from '@angular/platform-browser';  
      3.   
      4. @NgModule({  
      5.     imports: [BrowserModule]  
      6. })  
      7.   
      8. export class AppModule { }  
    • app.component.ts

      Create new typescript file and name it app.component.ts and add the following code.
      1. import { Component } from '@angular/core';  
      2. @Component({  
      3.     selector: 'my-app',  
      4.     template: '<h1>Angular 2 app using visual studio 2015</h1>'  
      5. })  
      6. export class AppComponent { }  
      Now it’s time we make some changes to app.module.ts file , call the component file we created in the module. A module can hold multiple components.

      The class exported (AppComponent) in the component is defined in the @NgModule under declarations and bootstrap.

      Change the module file as following.
      1. import { NgModule } from '@angular/core';  
      2. import { BrowserModule } from '@angular/platform-browser';  
      3. import { AppComponent } from './app.component';  
      4.   
      5. @NgModule({  
      6.     imports: [BrowserModule],  
      7.     declarations: [AppComponent],  
      8.     bootstrap: [AppComponent]  
      9. })  
      10. export class AppModule { }  
    • app.main.ts

      Create new typescript file and name it app.main.ts and add the following code.
      1. import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';  
      2. import { AppModule } from './app.module';  
      3. const platform = platformBrowserDynamic();  
      4. platform.bootstrapModule(AppModule);  


    “app.main.ts” file is used to bootstrap the angular application by calling the module.

  5. Create webpage to host the application

    Create an html file and name it mainpage.html that hosts the application.

    It will act as the start page displayed on the browser with the help of components

    Add the following code in mainpage.html file.
    1. <html>  
    2. <head>  
    3.     <title>Angular 2 Demo using Visual Studio 2015</title>  
    4.     <meta charset="UTF-8">  
    5.     <meta name="viewport" content="width=device-width, initial-scale=1">  
    6.     <link rel="stylesheet" href="styles.css">  
    7.   
    8.     <!-- 1. Load libraries -->  
    9.     <!-- Polyfill(s) for older browsers -->  
    10.     <script src="node_modules/core-js/client/shim.min.js"></script>  
    11.   
    12.     <script src="node_modules/zone.js/dist/zone.js"></script>  
    13.     <script src="node_modules/reflect-metadata/Reflect.js"></script>  
    14.     <script src="node_modules/systemjs/dist/system.src.js"></script>  
    15.   
    16.     <!-- 2. Configure SystemJS -->  
    17.     <script src="systemjs.config.js"></script>  
    18.     <script>  
    19.       System.import('app').catch(function(err){ console.error(err); });  
    20.     </script>  
    21. </head>  
    22.   
    23. <!-- 3. Display the application -->  
    24. <body>  
    25.     <my-app>Loading Angular 2 App using Visual Studio 2015...</my-app>  
    26. </body>  
    27. </html>  


  6. Install the packages defined in package.json and typings.json file

    In order for an angular application to work we need to install angular packages defined in package.json and typings.json file.

    Right click on package.json file and restore packages. This will install node_modules and typings folder in the project using npm.



    Wait for some time for the packages to be installed, you can check the status of the packages installed in output tab in visual studio.
    Once the packages are installed, you will find a message as “installing packages complete”



    Click on show files in solution explorer to see the node_modules and typings folder added into the project.



  7. Edit tsconfig.json file

    Edit code in tsconfig.json file and "compileOnSave": true” as shown below:
    1. {  
    2.   "compilerOptions": {  
    3.     "target""es5",  
    4.     "module""commonjs",  
    5.     "moduleResolution""node",  
    6.     "sourceMap"true,  
    7.     "emitDecoratorMetadata"true,  
    8.     "experimentalDecorators"true,  
    9.     "removeComments"false,  
    10.     "noImplicitAny"true,  
    11.     "suppressImplicitAnyIndexErrors"true,  
    12.     "typeRoots": [  
    13.       "node_modules/@types"  
    14.     ]  
    15.   },  
    16.   "compileOnSave"true  
    17. }  
    Set mainpage.html as startup page for the project.



    Press F5 to run the project. You will find the output as below.


  8. Try to change the text in “app.component.ts” file and refresh the browser and let me know your comments on what the output is.

If you are interested in downloading the code, please follow the below link and download he application

From GIT.
(or)
From bitbucket.

If you find any issue, let me know in the comments section.