<!DOCTYPE html>
<html>

  <head>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <title>angular2 playground</title>
    <link rel="stylesheet" href="style.css" />
    <script src="https://unpkg.com/zone.js@0.7.2?main=browser"></script>
    <script src="https://unpkg.com/reflect-metadata@0.1.8/Reflect.js"></script>
    <script src="https://unpkg.com/systemjs@0.19.39/dist/system.js"></script>
    <script src="config.js"></script>
    <script>
    System.import('app')
      .catch(console.error.bind(console));
  </script>
  </head>

  <body>
    <my-app>
    loading...
  </my-app>
  </body>

</html>
/* Styles go here */

### Angular Starter Plunker - Typescript
System.config({
  //use typescript for compilation
  transpiler: 'typescript',
  //typescript compiler options
  typescriptOptions: {
    emitDecoratorMetadata: true
  },
  paths: {
    'npm:': 'https://unpkg.com/'
  },
  //map tells the System loader where to look for things
  map: {
    
    'app': './src',
    
    '@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',
    
    '@angular/core/testing': 'npm:@angular/core/bundles/core-testing.umd.js',
    '@angular/common/testing': 'npm:@angular/common/bundles/common-testing.umd.js',
    '@angular/compiler/testing': 'npm:@angular/compiler/bundles/compiler-testing.umd.js',
    '@angular/platform-browser/testing': 'npm:@angular/platform-browser/bundles/platform-browser-testing.umd.js',
    '@angular/platform-browser-dynamic/testing': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic-testing.umd.js',
    '@angular/http/testing': 'npm:@angular/http/bundles/http-testing.umd.js',
    '@angular/router/testing': 'npm:@angular/router/bundles/router-testing.umd.js',
    
    'rxjs': 'npm:rxjs@5.0.0-rc.4',
    'typescript': 'npm:typescript@2.0.2/lib/typescript.js'
  },
  //packages defines our app package
  packages: {
    app: {
      main: './main.ts',
      defaultExtension: 'ts'
    },
    rxjs: {
      defaultExtension: 'js'
    }
  }
});
//main entry point
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {AppModule} from './app';

platformBrowserDynamic().bootstrapModule(AppModule)
import { Component, NgModule } from '@angular/core'
import { BrowserModule } from '@angular/platform-browser'
import { FormsModule }   from '@angular/forms';
import {RouterModule} from '@angular/router'

import { ROUTES, appComponents, appRouteGuards } from './app.routes'

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>{{title}}</h2> 
    </div>
    <router-outlet></router-outlet>
  `,
})
export class App {
  title = 'Deactivation Guard Bug';
}

@NgModule({
  imports: [ BrowserModule, FormsModule, RouterModule.forRoot(ROUTES)],
  declarations: [ App, ...appComponents ],
  providers: [ appRouteGuards ],
  bootstrap: [ App ]
})
export class AppModule {}
import { Routes } from '@angular/router';
import { RouteMain } from './RouteMain';
import { Route1 } from './route1';
import { Route2 } from './route2';
import { Route3 } from './route3';

import { CanDeactivateRoute3 } from './route3.guard';

export const ROUTES:Routes = [
    { path: '', component: RouteMain },
    { path: 'route/first', component: Route1 },
    { path: 'route/second', component: Route2 },
    { path: 'route/third', component: Route3, canDeactivate: [CanDeactivateRoute3] }];
    
export const appComponents = [ RouteMain, Route1, Route2, Route3 ];

export const appRouteGuards = [ CanDeactivateRoute3 ];
import {Component} from '@angular/core';
import { Location } from '@angular/common';
import { Router } from '@angular/router';

@Component({
  selector: 'route1',
  template: `
    <div>Route #1</div>
    <button (click)='goBack()'>Nav Back</button>
    <button (click)='onClick()'> Nav to route2</button>
  `,
})
export class Route1 {

  constructor(private _router: Router, private _location: Location) {}
  
  onClick() {
    //this._router.navigateByUrl("route/first").catch(err => console.error(err));
    this._router.navigate(['/route/second']).catch(err => console.log(err));
  }
  
  goBack() {
    this._location.back();
  }
  
}
import {Component} from '@angular/core';
import { Location } from '@angular/common';
import { Router } from '@angular/router';

@Component({
  selector: 'route2',
  template: `
    <div>Route #2</div>
    <button (click)='goBack()'>Nav Back</button>
    <button (click)='onClick()'> Nav to route3</button>
  `,
})
export class Route2 {

  constructor(private _router: Router, private _location: Location) {}
  
  onClick() {
    //this._router.navigateByUrl("route/first").catch(err => console.error(err));
    this._router.navigate(['/route/third']).catch(err => console.log(err));
  }
  
  goBack() {
    this._location.back();
  }
}
import {Component} from '@angular/core';
import { Location } from '@angular/common';
import { Router } from '@angular/router';

@Component({
  selector: 'route3',
  template: `
    <div>Route #3</div>
    <button (click)='goBack(true)'>Block Nav Back</button>
    <button (click)='goBack(false)'>Nav Back</button>
  `,
})
export class Route3 {
  public canNavBack:boolean = true;
  constructor(private _router: Router, private _location: Location) {}
  
  onClick() {
    //this._router.navigateByUrl("route/first").catch(err => console.error(err));
    this._router.navigate(['/route/first']).catch(err => console.log(err));
  }
  
  goBack(blockGuard: boolean) {
    this.canNavBack = !blockGuard;
    this._location.back();
  }
}
import {Component} from '@angular/core';
import { Router } from '@angular/router';


@Component({
  selector: 'routemain',
  template: `
    <div>
      <button (click)='onClick()'> Nav to route1</button>
    </div>
  `,
})
export class RouteMain {

  constructor(private _router: Router) {}
  
  onClick() {
    //this._router.navigateByUrl("route/first").catch(err => console.error(err));
    this._router.navigate(['/route/first']).catch(err => console.log(err));
  }
}
import { Injectable } from '@angular/core';
import { CanActivate, CanDeactivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable, Observer, Subscription, Subject } from 'rxjs/Rx';
import { of } from 'rxjs/observable/of';

import {Route3} from './route3';

@Injectable()
export class CanDeactivateRoute3 implements CanDeactivate<Route3> {
  canDeactivate(component: Route3, route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
        return of(component.canNavBack);
    }
}