<!DOCTYPE html>
<html>
<head>
<!-- We set the base href -->
<script>document.write('<base href="' + document.location + '" />');</script>
<title>Angular 2 Router</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSS file -->
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" >
<link rel="stylesheet" type="text/css" href="style.css">
<!-- IE polyfills, keep the order please -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.33.3/es6-shim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.20/system-polyfills.js"></script>
<!-- Agular 2 -->
<script src="https://code.angularjs.org/2.0.0-beta.7/angular2-polyfills.js"></script>
<script src="https://code.angularjs.org/tools/system.js"></script>
<script src="https://code.angularjs.org/tools/typescript.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.7/Rx.js"></script>
<script src="https://code.angularjs.org/2.0.0-beta.7/angular2.dev.js"></script>
<!-- Agular 2 Router -->
<script src="https://code.angularjs.org/2.0.0-beta.7/router.dev.js"></script>
<!-- Config Agular 2 and Typescript -->
<script>
System.config({
transpiler: 'typescript',
typescriptOptions: { emitDecoratorMetadata: true },
packages: {'app': {defaultExtension: 'ts'}}
});
System.import('app/main')
.then(null, console.error.bind(console));
</script>
</head>
<!-- Run the application -->
<body>
<h1>Angular 2 Router Example </h1>
<my-app>Loading Sample...</my-app>
<div style="padding-top:50px">
<a target="_blank" href="http://www.angulartypescript.com/angular-2-tutorial/" title="Angular 2 Tutorial">
<img src="http://www.angulartypescript.com/wp-content/uploads/2016/03/learn-more-angular-2.png" alt="Smiley face" height="200" width="500">
</a>
<ul class="nav nav-pills nav-stacked" >
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-tutorial/" title="Angular 2 Home"> Angular 2 Tutorial </a></li>
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-introduction/">Angular 2 Introduction</a></li>
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-architecture/">Angular 2 Architecture</a></li>
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-annotations/">Angular 2 Annotations</a></li>
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-getting-started/">Angular 2 Setup</a></li>
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-hello-world/">Angular 2 Hello World</a></li>
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-components/">Angular 2 Components</a></li>
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-template-syntax/">Angular 2 Template Syntax</a></li>
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-data-binding/">Angular 2 Data Binding</a></li>
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-forms/">Angular 2 Forms</a></li>
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-formbuilder-example/">Angular 2 Formbuilder</a></li>
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-router-example/">Angular 2 Router</a></li>
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-http-example-typescript/">Angular 2 HTTP</a></li>
<li><a target="_blank" href="http://www.angulartypescript.com/angular-2-services/">Angular 2 Service</a></li>
</ul>
</div>
</body>
</html>
<!--
Copyright 2016 angulartypescript.com. All Rights Reserved.
Everyone can use this source code; don’t forget to indicate the source please:
http://www.angulartypescript.com/
-->
/* Styles go here */
/**
* Created by Tareq Boulakjar. from angulartypescript.com
*/
import {bootstrap} from 'angular2/platform/browser';
import {ROUTER_PROVIDERS} from 'angular2/router';
import {RouteExampleAppComponent} from "./angular-2-router";
bootstrap( RouteExampleAppComponent, [ROUTER_PROVIDERS]);
/*
Copyright 2016 angulartypescript.com. All Rights Reserved.
Everyone can use this source code; don’t forget to indicate the source please:
http://www.angulartypescript.com/
*/
/**
* Created by Tareq Boulakjar. from angulartypescript.com
*/
import {Component} from 'angular2/core';
@Component({
selector: 'angular-2-about-us',
template: `
<h5 class="title">Angular 2 About Us Component</h5>
<div>
<p>
about us
</p>
</div>
`,
})
export class AboutUsComponent { }
/*
Copyright 2016 angulartypescript.com. All Rights Reserved.
Everyone can use this source code; don’t forget to indicate the source please:
http://www.angulartypescript.com/
*/
/**
* Created by Tareq Boulakjar. from angulartypescript.com
*/
import {Component} from 'angular2/core';
import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router';
import {ContactComponent} from "./contact.component";
import {ProductsComponent} from "./products.component";
import {AboutUsComponent} from "./about-us.component";
import {ProductDetailComponent} from "./product-detail.component";
import {CarService} from "./car.service";
import {ConfirmDialogService} from "./confirm-dialog.service";
@Component({
selector: 'my-app',
template: `
<h3 class="title">Angular 2 Router Example</h3>
<nav>
<a [routerLink]="['Products']">Products</a>
<a [routerLink]="['AboutUs']">About Us</a>
<a [routerLink]="['Contact']">Contact</a>
</nav>
<router-outlet></router-outlet>
`,
providers: [ConfirmDialogService,CarService],
directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
{
path: '/contact',
name: 'Contact',
component: ContactComponent,
useAsDefault: true
},
{
path: '/products',
name: 'Products',
component: ProductsComponent
},
{
path: '/about-us',
name: 'AboutUs',
component: AboutUsComponent
},
{path: '/car/:id', name: 'CarDetail', component: ProductDetailComponent},
])
export class RouteExampleAppComponent { }
/*
Copyright 2016 angulartypescript.com. All Rights Reserved.
Everyone can use this source code; don’t forget to indicate the source please:
http://www.angulartypescript.com/
*/
/**
* Created by Tareq Boulakjar. from angulartypescript.com
*/
import {Injectable} from 'angular2/core';
export class Car {
constructor(public id: number, public name: string) { }
}
@Injectable()
export class CarService {
getCars() { return carsPromise; }
getCar(id: number | string) {
return carsPromise
.then(cars => cars.filter(h => h.id === +id)[0]);
}
}
var CARS = [
new Car(1, 'Audi 1'),
new Car(2, 'Audi 2'),
new Car(3, 'Audi 3'),
new Car(4, 'Audi 4'),
];
var carsPromise = Promise.resolve(CARS);
/*
Copyright 2016 angulartypescript.com. All Rights Reserved.
Everyone can use this source code; don’t forget to indicate the source please:
http://www.angulartypescript.com/
*/
/**
* Created by Tareq Boulakjar. from angulartypescript.com
*/
import {Injectable} from 'angular2/core';
@Injectable()
export class ConfirmDialogService {
/**
* Ask a user to confirm or cancel an action, this method accept the message related to action as a parameter
* It returns a promise resolving to `true`=confirm or `false`=cancel (YES/NO) answers
*/
confirm(message?:string) {
return new Promise<boolean>((resolve, reject) =>
resolve(window.confirm(message || 'Okey?')));
};
}
/*
Copyright 2016 angulartypescript.com. All Rights Reserved.
Everyone can use this source code; don’t forget to indicate the source please:
http://www.angulartypescript.com/
*/
/**
* Created by Tareq Boulakjar. from angulartypescript.com
*/
import {Component} from 'angular2/core';
@Component({
selector: 'angular-2-contact',
template: `
<h5 class="title">Angular 2 Contact Component</h5>
<div>
<label>Your Email :</label>
<input type="text" placeholder="enter your email">
</div>
`,
})
export class ContactComponent { }
/*
Copyright 2016 angulartypescript.com. All Rights Reserved.
Everyone can use this source code; don’t forget to indicate the source please:
http://www.angulartypescript.com/
*/
/**
* Created by Tareq Boulakjar. from angulartypescript.com
*/
import {Component, OnInit} from 'angular2/core';
import {Car, CarService} from './car.service';
import {RouteParams, Router} from 'angular2/router';
import {CanDeactivate, ComponentInstruction} from 'angular2/router';
import {ConfirmDialogService} from "./confirm-dialog.service";
@Component({
template:
`
<h3>Angular 2 - Car Detail</h3>
<div *ngIf="car">
<h3>"{{car.name}}"</h3>
<div>
<label>Car Id: </label>{{car.id}}</div>
<div>
<label>Car Name: </label>
<input [(ngModel)]="carNameEdit" placeholder="name"/>
</div>
<button (click)="backToProductsList()">Back</button>
</div>
`,
})
export class ProductDetailComponent implements OnInit ,CanDeactivate {
car: Car;
carNameEdit:string;
constructor(
private _router:Router,
private _routeParams:RouteParams,
private _service:CarService,
private _dialog: ConfirmDialogService
){}
routerCanDeactivate(next: ComponentInstruction, prev: ComponentInstruction) : any {
// Allow navigation (`true`) if no car exist or the car is unmodified.
if (!this.car || this.car.name === this.carNameEdit) {
return true;
}
// if the user has changed something, then ask before you leave : )
// our servicee ConfirmDialogService returns a promise which resolves to true or false (YES/NO) when the user decides what to do.
return this._dialog.confirm('Ignore changes (are you sure you want to leave this window) ?');
}
ngOnInit() {
let id = this._routeParams.get('id');
this._service.getCar(id).then(car => {
if (car) {
this.carNameEdit = car.name;
this.car = car;
} else { // the car not found , go back to the car list view
this.backToProductsList();
}
});
}
backToProductsList() {
let carId = this.car ? this.car.id : null;
this._router.navigate(['Products', {id: carId} ]);
}
}
/*
Copyright 2016 angulartypescript.com. All Rights Reserved.
Everyone can use this source code; don’t forget to indicate the source please:
http://www.angulartypescript.com/
*/
/**
* Created by Tareq Boulakjar. from angulartypescript.com
*/
import {Component} from 'angular2/core';
import {Router, RouteParams} from 'angular2/router';
import {Car} from "./car.service";
import {CarService} from "./car.service";
@Component({
selector: 'angular-2-products',
template: `
<h5 class="title">Angular 2 Products Component</h5>
<div>
<ul *ngFor="#car of myCarList">
<li (click)="onSelectCar(car)">
<span class="badge">{{car.id}}</span> {{car.name}}
</li>
</ul>
</div>
`,
})
export class ProductsComponent {
myCarList: Car[]; // we carService services
private _theSelectedCar:number;// when we come back from car Detail we know witch car was selected.
constructor(
private _service: CarService,
private _router: Router,
routeParams: RouteParams) {
this._theSelectedCar = +routeParams.get('id');
}
onSelectCar(car) {
this._router.navigate( ['CarDetail', { id: car.id }] );
}
ngOnInit() {
this._service.getCars().then(cars => this.myCarList = cars)
}
}
/*
Copyright 2016 angulartypescript.com. All Rights Reserved.
Everyone can use this source code; don’t forget to indicate the source please:
http://www.angulartypescript.com/
*/