Angularjs 2 - Updating template or component does not update the UI

4.9k views Asked by At

I am coming from angular 1 background where everything worked so smoothly and having some serious issues even making a basic application work in Visual Studio with angular 2. I created an empty project in visual studio and followed the "5 minute tutorial" very carefully. It came to the point where it started working finally but I observed an issue with updating the UI when I change the template in app.component.ts, nothing is being picked up on the browser. I had restarted and rerun the files but still the old output is being presented on the browser.

Following is my code:

In tsconfig.json

{
  "compileOnSave": true,
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [ "es2015", "dom" ],
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true
  }
}

Systemjs.config.js:

/**
 * System configuration for Angular 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',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
    },
    // 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'
      }
    }
  });
})(this);

My index.html has the following contents

<!DOCTYPE html>
<html>
  <head>
    <title>Angular QuickStart</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="styles.css">

      <!-- Polyfill(s) for older browsers -->
    <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/reflect-metadata/Reflect.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>

    <script src="systemjs.config.js"></script>
    <script>
      System.import('app').catch(function(err){ console.error(err); });
    </script>
  </head>

  <body>
    <my-app>Loading</my-app>
  </body>
</html>

In app.module.ts, I have

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 { }

In app.component.ts:

import { Component } from "@angular/core";
@Component({
    selector: "my-app",
    template: "<h2>Initiating {{angularVersion}}</h2>"
})
export class AppComponent {
    angularVersion = "Angular2.0";
}

and finally in main.ts, I have:

import { platformBrowserDynamic } from "@angular/platform-browser-dynamic";
import { AppModule } from "./app.module";
platformBrowserDynamic().bootstrapModule(AppModule);

This displays the output as "Initiating Angular2.0" when I set index.html as startup file in visual studio 2015 and run the project. My problem is, even if I update angularVersion variable to 2.1 or change anything else (from h2 to p etc) and rerun the file, It would still present me "Initiating Angular2.0". Even hardcoding the Template won't update anything. I don't have any idea what I am doing wrong but it has started to frustrate a lot now. Any help please?

2

There are 2 answers

1
Ali Baig On BEST ANSWER

Hit F12 in your browser to bring up the Developer Tools. Disable the Cache as shown below

enter image description here

Secondly, I had to add this section just above tag in web.config to disable cache

<location path="app">
    <system.webServer>
      <staticContent>
        <clientCache cacheControlMode="DisableCache"/>
      </staticContent>
    </system.webServer>
</location>

Source: See here

0
DNK On

I changed in tsconfig.json:

compileOnSave value from false to true and it worked for me.