<!DOCTYPE html>
<html>
<head>
<link data-require="bootstrap-css@3.3.6" data-semver="3.3.6" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.css" />
<script>
document.write('<base href="' + document.location + '" />');
</script>
<title>Breadcrumb using Angular 2 Router</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<script src="https://unpkg.com/zone.js@0.6.21/dist/zone.js"></script>
<script src="https://unpkg.com/reflect-metadata@0.1.3/Reflect.js"></script>
<script src="https://unpkg.com/systemjs@0.19.31/dist/system.js"></script>
<script src="https://unpkg.com/typescript@1.8.10/lib/typescript.js"></script>
<script src="config.js"></script>
<script>
System.import('app')
.catch(console.error.bind(console));
</script>
</head>
<body>
<div class="container">
<my-app>
loading...
</my-app>
</div>
</body>
</html>
# Angular 2.0.0 + TypeScript Plunker Starter Kit
I couldn't find this elsewhere, so I created one.
This is a simple Angular 2.0.0 (final) + TypeScript plunker. Fork it.
System.config({
transpiler: 'typescript',
typescriptOptions: {
emitDecoratorMetadata: true
},
map: {
app: "./src",
'@angular/core': 'npm:@angular/core@2.0.0/bundles/core.umd.js',
'@angular/common': 'npm:@angular/common@2.0.0/bundles/common.umd.js',
'@angular/compiler': 'npm:@angular/compiler@2.0.0/bundles/compiler.umd.js',
'@angular/platform-browser': 'npm:@angular/platform-browser@2.0.0/bundles/platform-browser.umd.js',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic@2.0.0/bundles/platform-browser-dynamic.umd.js',
'@angular/http': 'npm:@angular/http@2.0.0/bundles/http.umd.js',
'@angular/router': 'npm:@angular/router@3.0.0/bundles/router.umd.js',
'@angular/forms': 'npm:@angular/forms@2.0.0/bundles/forms.umd.js',
'@angular/core/testing': 'npm:@angular/core@2.0.0/bundles/core-testing.umd.js',
'@angular/common/testing': 'npm:@angular/common/bundles@2.0.0/common-testing.umd.js',
'@angular/compiler/testing': 'npm:@angular/compiler@2.0.0/bundles/compiler-testing.umd.js',
'@angular/platform-browser/testing': 'npm:@angular/platform-browser@2.0.0/bundles/platform-browser-testing.umd.js',
'@angular/platform-browser-dynamic/testing': 'npm:@angular/platform-browser-dynamic@2.0.0/bundles/platform-browser-dynamic-testing.umd.js',
'@angular/http/testing': 'npm:@angular/http@2.0.0/bundles/http-testing.umd.js',
'@angular/router/testing': 'npm:@angular/router@3.0.0/bundles/router-testing.umd.js',
'rxjs': 'npm:rxjs'
},
//packages defines our app package
packages: {
app: {
main: './main.ts',
defaultExtension: 'ts'
},
rxjs: {
defaultExtension: 'js'
}
},
paths: {
'npm:': 'https://unpkg.com/'
}
});
//main entry point
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
platformBrowserDynamic().bootstrapModule(AppModule)
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from "./app.component";
import { BreadcrumbComponent } from './breadcrumb/breadcrumb.component';
import { RootComponent } from "./root/root.component";
import { IndexComponent } from "./index/index.component";
import { SigninComponent } from "./signin/signin.component";
import { SignupComponent } from "./signup/signup.component";
import { routing } from "./app.routing.module";
@NgModule({
imports: [
BrowserModule,
routing
],
declarations: [
AppComponent,
BreadcrumbComponent,
IndexComponent,
RootComponent,
SigninComponent,
SignupComponent
],
bootstrap: [ AppComponent ]
})
export class AppModule {}
import { ModuleWithProviders } from "@angular/core";
import { Routes, RouterModule } from "@angular/router";
import { IndexComponent } from "./index/index.component";
import { RootComponent } from "./root/root.component";
import { SigninComponent } from "./signin/signin.component";
import { SignupComponent } from "./signup/signup.component";
const routes: Routes = [
{
path: "",
component: RootComponent,
children: [
{
path: "signin",
component: SigninComponent,
data: {
breadcrumb: "Sign In"
}
},
{
path: "signup",
component: SignupComponent,
data: {
breadcrumb: "Sign Up"
}
},
{
path: "",
component: IndexComponent
}
]
},
];
export const routing: ModuleWithProviders = RouterModule.forRoot(routes);
import { Component, OnInit } from "@angular/core";
@Component({
template: `
<router-outlet></router-outlet>
`
})
export class RootComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
import { Component, OnInit } from "@angular/core";
@Component({
template: `
<h3>Sign Up</h3>
<form>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Email">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
`
})
export class SignupComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
import { Component, OnInit } from "@angular/core";
@Component({
template: `
<h3>Sign In</h3>
<form>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Email">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
`
})
export class SigninComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<h1>Killer App</h1>
<breadcrumb></breadcrumb>
<router-outlet></router-outlet>
`
})
export class AppComponent implements OnInit {
constructor() {}
ngOnInit() {}
}
import { Component, OnInit } from "@angular/core";
@Component({
template: `
<a routerLink="/signin" class="btn btn-block btn-primary">Sign In</a>
<a routerLink="/signup" class="btn btn-block btn-primary">Sign Up</a>
`
})
export class IndexComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
import { Component, OnInit } from "@angular/core";
import { Router, ActivatedRoute, NavigationEnd, Params, PRIMARY_OUTLET } from "@angular/router";
import "rxjs/add/operator/filter";
interface IBreadcrumb {
label: string;
params?: Params;
url: string;
}
@Component({
selector: "breadcrumb",
template: `
<ol class="breadcrumb">
<li><a routerLink="" class="breadcrumb">Home</a></li>
<li *ngFor="let breadcrumb of breadcrumbs">
<a [routerLink]="[breadcrumb.url, breadcrumb.params]">{{breadcrumb.label}}</a>
</li>
</ol>
`
})
export class BreadcrumbComponent implements OnInit {
public breadcrumbs: IBreadcrumb[];
constructor(
private activatedRoute: ActivatedRoute,
private router: Router
) {
this.breadcrumbs = [];
}
ngOnInit() {
const ROUTE_DATA_BREADCRUMB: string = "breadcrumb";
//subscribe to the NavigationEnd event
this.router.events.filter(event => event instanceof NavigationEnd).subscribe(event => {
//set breadcrumbs
let root: ActivatedRoute = this.activatedRoute.root;
this.breadcrumbs = this.getBreadcrumbs(root);
});
}
private getBreadcrumbs(route: ActivatedRoute, url: string="", breadcrumbs: IBreadcrumb[]=[]): IBreadcrumb[] {
const ROUTE_DATA_BREADCRUMB: string = "breadcrumb";
//get the child routes
let children: ActivatedRoute[] = route.children;
//return if there are no more children
if (children.length === 0) {
return breadcrumbs;
}
//iterate over each children
for (let child of children) {
//verify primary route
if (child.outlet !== PRIMARY_OUTLET) {
continue;
}
//verify the custom data property "breadcrumb" is specified on the route
if (!child.snapshot.data.hasOwnProperty(ROUTE_DATA_BREADCRUMB)) {
return this.getBreadcrumbs(child, url, breadcrumbs);
}
//get the route's URL segment
let routeURL: string = child.snapshot.url.map(segment => segment.path).join("/");
//append route URL to URL
url += `/${routeURL}`;
//add breadcrumb
let breadcrumb: IBreadcrumb = {
label: child.snapshot.data[ROUTE_DATA_BREADCRUMB],
params: child.snapshot.params,
url: url
};
breadcrumbs.push(breadcrumb);
//recursive
return this.getBreadcrumbs(child, url, breadcrumbs);
}
//we should never get here, but just in case
return breadcrumbs;
}
}