Support us .Net Basics C# SQL ASP.NET ADO.NET MVC Slides C# Programs Subscribe Buy DVD

Run angular 2 app using f5 from visual studio


Suggested Videos

Part 1 - Introduction to Angular 2 | Text | Slides
Part 2 - Setting up Angular 2 in Visual Studio | Text | Slides

In this video we will discuss how to run angular 2 application from visual studio using F5 or CTRL + F5.

This is continuation to Part 2. Please watch Part 2 from Angular 2 tutorial before proceeding.



At the moment, if we run the application from Visual Studio, using F5 or CTRL+F5, we get the message "Loading AppComponent content here ..." but nothing happens beyond that. To be able to run the application using F5 or CTRL+F5 we need to make the following changes.



1. Launch browser developers tools by pressing F12. Notice we have "404 Not Found" errors for the following files.
  • styles.css
  • systemjs.config.js
  • main.js
angular 2 loading appcomponent content here

All these files are present in "src" folder. So to fix these "404 Not Found" errors, in index.html file, change <base href="/"> to <base href="/src/">

2. Save the changes and reload the page. At this point we get another set of "404 Not Found" errors for the following files.
  • shim.min.js
  • zone.js
  • system.src.js
angular 2 node_modules 404

<script src="node_modules/core-js/client/shim.min.js"></script>
<script src="node_modules/zone.js/dist/zone.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>

To fix these errors, in index.html change the above script references as shown below. Notice, we have included "/" just before node_modules

<script src="/node_modules/core-js/client/shim.min.js"></script>
<script src="/node_modules/zone.js/dist/zone.js"></script>
<script src="/node_modules/systemjs/dist/system.src.js"></script>

Also in systemjs.config.js file, CHANGE
'npm:': 'node_modules/'  TO  'npm:''/node_modules/'

At this point reload the page and you will see "Hello Angular" message without any errors.

One important point to keep in mind is that, now we will not be able to run the application using "npm start" command.

We still have one more issue. Let us first understand the issue.
1. Expand "app" folder. This folder is inside "src" folder
2. Open "app.component.ts" file 
3. Set name="Angular 2!" from name="Angular"
4. Save the changes and reload the web page 
5. Notice, we do not see the changes on the web page
6. However, if we run the application by pressing F5 or CTRL+F5 from visual studio we see the changes in the browser.

So what is the issue?
TypeScript is not complied to JavaScript when we save the file and this the reason we do not see the changes in the browser. However, when we run the application by pressing F5 or CTRL+F5 from visual studio TypeScript is compiled to JavaScript and we see the changes.

If you want Visual Studio to compile TypeScript to JavaScript when the changes are saved, we need to turn this feature "ON" by including the following setting in tsconfig.json file. You will find this file in "src" folder. Notice we have set "compileOnSave" to true. With this change tsconfig.json file looks as shown below.
{
  "compileOnSave": true,
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [ "es2015", "dom" ],
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true
  }
}

At this point TypeScript is automatically compiled to JavaScript when the file is saved, so the changes are reflected in the browser when the page is reloaded.

At the moment, we are using Visual Studio built-in IIS express server. In a later video in this course we will discuss how to use full blown IIS instead of Visual Studi built-in IIS express.

Should I learn AngularJS1 before learning Angular 2?
NO, Angular 2 is completely rewritten and very different from AngularJS1, so there is no need to learn AngularJS 1 before learning Angular 2.


Angular 2 tutorial for beginners

10 comments:

  1. Hello Venkat,

    I have done all the changes specified by you still i am not able to run the solution properly i am this below error.

    node_modules/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js Failed to load resource: the server responded with a status of 404 (Not Found)


    index.html:18 Error: (SystemJS) XHR error (404 Not Found) loading http://localhost:51412/src/node_modules/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js
    Error: XHR error (404 Not Found)

    Please let me know what i am missing.


    ReplyDelete
    Replies
    1. Close Visual Studio, delete the solution folder and recreate the project from scratch by following the steps in Part 2 and Part 3 of this video series, and hopefully the error goes away.

      Delete
  2. Hi Venkat,

    I have done all changes as per your guide but still I am not able to run my project with F5 error ,

    Error: (SystemJS) XHR error (404 Not Found) loading http://localhost:52467/src/node_modules/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js
    Error: XHR error (404 Not Found) loading http://localhost:52467/src/node_modules/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js
    at XMLHttpRequest.wrapFn [as __zone_symbol___onreadystatechange] (http://localhost:52467/node_modules/zone.js/dist/zone.js:1055:39)
    at ZoneDelegate.invokeTask (http://localhost:52467/node_modules/zone.js/dist/zone.js:424:31)
    at Zone.runTask (http://localhost:52467/node_modules/zone.js/dist/zone.js:191:47)
    at XMLHttpRequest.ZoneTask.invoke (http://localhost:52467/node_modules/zone.js/dist/zone.js:486:38)
    Error loading http://localhost:52467/src/node_modules/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js as "@angular/platform-browser-dynamic" from http://localhost:52467/src/main.js
    at XMLHttpRequest.wrapFn [as __zone_symbol___onreadystatechange] (http://localhost:52467/node_modules/zone.js/dist/zone.js:1055:39)
    at ZoneDelegate.invokeTask (http://localhost:52467/node_modules/zone.js/dist/zone.js:424:31)
    at Zone.runTask (http://localhost:52467/node_modules/zone.js/dist/zone.js:191:47)
    at XMLHttpRequest.ZoneTask.invoke (http://localhost:52467/node_modules/zone.js/dist/zone.js:486:38)
    Error loading http://localhost:52467/src/node_modules/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js as "@angular/platform-browser-dynamic" from http://localhost:52467/src/main.js
    (anonymous) @ index.html:18
    ZoneDelegate.invoke @ zone.js:391
    Zone.run @ zone.js:141
    (anonymous) @ zone.js:817
    ZoneDelegate.invokeTask @ zone.js:424
    Zone.runTask @ zone.js:191
    drainMicroTaskQueue @ zone.js:584
    ZoneTask.invoke @ zone.js:490
    zone.js:2224 GET http://localhost:52467/src/node_modules/@angular/core/bundles/core.umd.js 404 (Not Found)
    scheduleTask @ zone.js:2224
    ZoneDelegate.scheduleTask @ zone.js:410
    Zone.scheduleTask @ zone.js:235
    Zone.scheduleMacroTask @ zone.js:258
    (anonymous) @ zone.js:2248
    proto.(anonymous function) @ zone.js:1425
    fetchTextFromURL @ system.src.js:1051
    (anonymous) @ system.src.js:1778
    ZoneAwarePromise @ zone.js:833
    (anonymous) @ system.src.js:1777
    (anonymous) @ system.src.js:2806
    (anonymous) @ system.src.js:3384
    (anonymous) @ system.src.js:3707
    (anonymous) @ system.src.js:4099
    (anonymous) @ system.src.js:4562
    (anonymous) @ system.src.js:4831
    (anonymous) @ system.src.js:407
    ZoneDelegate.invoke @ zone.js:391
    Zone.run @ zone.js:141
    (anonymous) @ zone.js:817
    ZoneDelegate.invokeTask @ zone.js:424
    Zone.runTask @ zone.js:191
    drainMicroTaskQueue @ zone.js:584
    ZoneTask.invoke @ zone.js:490
    zone.js:2224 GET http://localhost:52467/src/node_modules/@angular/platform-browser/bundles/platform-browser.umd.js 404 (Not Found)
    scheduleTask @ zone.js:2224
    ZoneDelegate.scheduleTask @ zone.js:410
    Zone.scheduleTask @ zone.js:235
    Zone.scheduleMacroTask @ zone.js:258
    (anonymous) @ zone.js:2248
    proto.(anonymous function) @ zone.js:1425
    fetchTextFromURL @ system.src.js:1051
    (anonymous) @ system.src.js:1778
    ZoneAwarePromise @ zone.js:833
    (anonymous) @ system.src.js:1777
    (anonymous) @ system.src.js:2806
    (anonymous) @ system.src.js:3384
    (anonymous) @ system.src.js:3707
    (anonymous) @ system.src.js:4099
    (anonymous) @ system.src.js:4562
    (anonymous) @ system.src.js:4831
    (anonymous) @ system.src.js:407
    ZoneDelegate.invoke @ zone.js:391
    Zone.run @ zone.js:141
    (anonymous) @ zone.js:817
    ZoneDelegate.invokeTask @ zone.js:424
    Zone.runTask @ zone.js:191
    drainMicroTaskQueue @ zone.js:584
    ZoneTask.invoke @ zone.js:490

    ReplyDelete
    Replies
    1. Hi,
      if you open lite server . close it out first and reload the page,it works for me.
      angular gives preference to the lite server

      Delete
    2. Close Visual Studio, delete the solution folder and recreate the project from scratch by following the steps in Part 2 and Part 3 of this video series, and hopefully the error goes away.

      Delete
    3. Hi Venkat,
      Thanks my issue is fixed.

      Delete
    4. If I use ASP target framework 4.6.1, I get the same error message as Anoop and Anonymous. I changed the target framework to 4.5.2 and it worked! I don't know id this works on 4.6.1 and later. If anyone knows, please let us know!

      Delete
  3. I have the same problem with platform-browser-dynamic.umd.js. Please reply

    ReplyDelete
  4. How can i run angualr 2 using webpack in visual Studio (F5 key)

    ReplyDelete
  5. I changed the target framework to 4.6.1 and it works now. Sorry I stand corrected from my earlier post.

    ReplyDelete

If you like this website, please share with your friends on facebook and Google+ and recommend us on google using the g+1 button on the top right hand corner.