All Projects → Drag13 → MvcWithAngular2

Drag13 / MvcWithAngular2

Licence: other
This is web project configured for working with angular 4.0 with Microsoft Visual Studio 2015

Programming Languages

typescript
32286 projects
HTML
75241 projects
javascript
184084 projects - #8 most used programming language
C#
18002 projects
CSS
56736 projects

AngularWebAppTemplate

This is web project configured for working with angular 4.0 RELEASE and Microsoft Visual Studio 2015

Short instruction how to run angular 4 with MS Visual Studio 2015

Content

  1. Check list
  2. Installing Angular from scratch
  3. Problems and solutions
  4. IIS settings for SPA application

Check list

  1. Manually restore NuGet packages for the project.
  2. Install NPM Task Runner. This will allow you to run tasks described in package.json
  3. Check npm version for Studio. Using NPM Task Runner execute "getNpmVersion" command from package.json If it is higher then 3.X.X - all is ok. If not - see "NPM version lower than 3"chapter from readme.html.
  4. Check Node.JS version for Studio. Using NPM Task Runner execute "getNodeVersion" command from package.json If it is higher then 5.X.X - all is ok. If not - see "Node.JS version lower than 5" chapter from readme.html.
  5. IMPORTANT: Run !install command from the task runner. For unknown reason taskrunner does not see angular files from VS so you should use this command.
  6. If all is ok - run application and have fun. If smth wrong happened - go to the Troubleshooting" chapter.
  7. If you still have problems - try to install all from null.

Installing Angular 4.0 Release version for MS Visual Studio 2015 from null.

  1. Install NPM Task Runner and Package Installer.
  2. Create new empty web project.
  3. Add package.json file to the solution root with next content
    {
        "name": "myproject",
        "version": "1.0.0",
        "devDependencies": {
            },
        "scripts": {
            "updateNpm": "npm install npm@latest"
        }
    } 
    
  4. Run "updateNpm" command from your taskrunner. Even if you have last npm - do this. This is important. If you have already installed Node.js before skip this step. If not - go the Node.js and install latest version of node.js. Inside Visual studio, go the Tools -> Options -> Projects and Solutions -> External web tools and set a path to the propper node.js version. Do not forget to move your new path up the hill.
  5. Update your package json with dependencies, angular 2 needed. (With little overhead for the old browswers)
    {
      "name": "myproject",
      "repository": "https://github.com/Drag13/MvcWithAngular2",
      "license": "MIT",
      "version": "1.5.0",
      "devDependencies": {
        "typescript": "2.2.2",
        "typings": "2.1.1"
      },
    

    "dependencies": { "@angular/common": "~4.0.0", "@angular/compiler": "~4.0.0", "@angular/core": "~4.0.0", "@angular/forms": "~4.0.0", "@angular/http": "~4.0.0", "@angular/platform-browser": "~4.0.0", "@angular/platform-browser-dynamic": "~4.0.0", "@angular/router": "~4.0.0",

    "angular-in-memory-web-api": "~0.3.0",
    "systemjs": "0.19.40",
    "core-js": "^2.4.1",
    "rxjs": "5.0.1",
    "zone.js": "^0.8.4"
    

    },

    "scripts": { "postinstall": "typings install", "typings": "typings", "cmd": "npm typescript", "getNpmVersion": "npm -v", "getNodeVersion": "node -v", "updateNpm": "npm install npm@latest", "!install": "npm install" } }

  6. Run install command from the Task runner.
  7. Create app folder in the root of your solution. Add app.components.ts to the app folder.
  8. Configure typescript for Visual studio. Close your project and open .csproj file in the text editor. Find PropertyGroup section and add two additional options:
    <TypeScriptExperimentalDecorators>true</TypeScriptExperimentalDecorators>
    <TypeScriptEmitDecoratorMetadata>true</TypeScriptEmitDecoratorMetadata>
  9. Save changes and open solution.
  10. Open your project properties and go to the TypeScript setting. Pick CommonJS as module system.
  11. Update app.component.ts with this code.
    import { Component } from '@angular/core';
    @Component({
        selector: 'my-app',
        template: 'My First Angular App'
    })
    export class AppComponent { }  
  12. Add main.ts to the app folder.
    import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
    import { AppModule } from './app.module';
    const platform = platformBrowserDynamic();
    platform.bootstrapModule(AppModule);
  13. Add app.module.ts near the main.ts with the following code
    import { NgModule }      from '@angular/core';
         import { BrowserModule } from '@angular/platform-browser';
         import { AppComponent }   from './app.component';
        @NgModule({
            imports: [BrowserModule],
            declarations: [AppComponent],
            bootstrap: [AppComponent]
    })
    export class AppModule { }
    
  14. Add index.html to the root of your project. Code for index.html could be found inside the repository.
  15. Add systemjs.config.js to the root with next code.
    /**
     * System configuration for Angular 2 samples
     * Adjust as necessary for your application needs.
     */
    (function (global) {
        System.config({
            paths: {
                // paths serve as alias
                'npm:': 'node_modules/'
            },
            // map tells the System loader where to look for things
            map: {
                // our app is within the app folder
                app: 'app',
                // angular bundles
                '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
                '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
                '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
                '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
                '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
                '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
                '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
                '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
                // other libraries
                'rxjs': 'npm:rxjs',
                'angular2-in-memory-web-api': 'npm:angular2-in-memory-web-api',
            },
            // packages tells the System loader how to load when no filename and/or no extension
            packages: {
                app: {
                    main: './main.js',
                    defaultExtension: 'js'
                },
                rxjs: {
                    defaultExtension: 'js'
                },
                'angular2-in-memory-web-api': {
                    main: './index.js',
                    defaultExtension: 'js'
                }
            }
        });
    })(this);
    
  16. Press F5 to run your app. If you see My First Angular 2 App on your screen - all is ok. You can proceed developing.

Problems and solutions

  1. Problem: NPM version lower than 3

    Solution: Find the location where the global NPM was installed (you can execute the npm root -g command). Then substitute the pathway to Node NPM in the PATH environmental variable with the pathway to the global NPM. Do not forget to do it for the system and for the user, as well as to reboot your workstation.

  2. Problem: Node.JS version lower than 5

    Solution: Find the location, where your node.js is installed, and copy the path there. Now go to Tools -> Options -> Projects and Solutions -> External web tools and add a new path there which directs the Studio to the necessary node version (if there isn’t one, just download and install it). Don’t forget to bring the new path to the top. Restart the Studio

  3. Problem: All package installing with errors.

    Solution: Try command: "npm cache clear". This will remove all cached before packages and allows you to get clean and fresh latest packages.

  4. Problem: VS throws compile error like: "TS2304: Cannot find name 'Map'".

    Solution: Change TypeScript ECMAscript version to 6 version. You can find it at Project properties, TypeScript compile options.

IIS settings for SPA application

Using app as SPA we have to configure IIS server to ignore requests except root and api. This can be achieved with adding custom rule in web.config. Example of this you can find below.

    <rewrite>
      <rules><
        <rule name="AngularJS Routes" stopProcessing="true">
            <match url=".*" />
                <conditions logicalGrouping="MatchAll">
                 <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                 <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                 <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
                 </conditions>
            <action type="Rewrite" url="/" />
        </rule>
      </rules>
    </rewrite>

More info can be find here: IIS Url Rewrite Module

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].