diff --git a/app/app.component.html b/app/app.component.html index e5fc017..85ae5ad 100644 --- a/app/app.component.html +++ b/app/app.component.html @@ -1,8 +1,3 @@ -

Hello!

- - - +Dashboard +Ducks + diff --git a/app/app.component.ts b/app/app.component.ts index 1bac516..c530bc2 100644 --- a/app/app.component.ts +++ b/app/app.component.ts @@ -1,34 +1,7 @@ -import { Component, OnInit } from "@angular/core"; - -import { Duck } from "./models"; -import { DuckService } from "./duck.service"; +import { Component } from "@angular/core"; @Component({ selector: "duckbook-front", - templateUrl: "/app/app.component.html", - providers: [ DuckService ] + templateUrl: "/app/app.component.html" }) -export class AppComponent implements OnInit { - ducks = []; - selectedDuck: Duck; - - constructor(private duckService: DuckService) {} - - ngOnInit(): void { - this.getDucks(); - } - - getDucks(): void { - this.duckService.getDucks().then(ducks => this.ducks = ducks); - } - - onSelect(duck: Duck): void { - console.log(duck); - this.selectedDuck = duck; - } - - duck: Duck = { - id: 1, - color: 'yellow' - }; -} +export class AppComponent {} diff --git a/app/app.module.ts b/app/app.module.ts index 8d84997..ed51bb0 100644 --- a/app/app.module.ts +++ b/app/app.module.ts @@ -2,18 +2,28 @@ import { NgModule } from "@angular/core"; import { BrowserModule } from "@angular/platform-browser"; import { FormsModule } from "@angular/forms"; +import { routing } from "./app.routing"; + import { AppComponent } from "./app.component"; +import { DucksComponent } from "./ducks.component"; import { DuckDetailComponent } from "./duck-detail.component"; +import { DashboardComponent } from "./dashboard.component"; + +import { DuckService } from "./duck.service"; @NgModule({ imports: [ BrowserModule, - FormsModule + FormsModule, + routing ], declarations: [ AppComponent, - DuckDetailComponent + DucksComponent, + DuckDetailComponent, + DashboardComponent ], + providers: [ DuckService ], bootstrap: [ AppComponent ] }) export class AppModule {} diff --git a/app/app.routing.ts b/app/app.routing.ts new file mode 100644 index 0000000..2ccdf5c --- /dev/null +++ b/app/app.routing.ts @@ -0,0 +1,28 @@ +import { ModuleWithProviders } from "@angular/core"; +import { Routes, RouterModule } from "@angular/router"; + +import { DucksComponent } from "./ducks.component"; +import { DuckDetailComponent } from "./duck-detail.component"; +import { DashboardComponent } from "./dashboard.component"; + +const appRoutes: Routes = [ + { + path: "", + redirectTo: "/dashboard", + pathMatch: "full" + }, + { + path: "dashboard", + component: DashboardComponent + }, + { + path: "ducks", + component: DucksComponent + }, + { + path: "detail/:id", + component: DuckDetailComponent + } +] + +export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes); diff --git a/app/dashboard.component.html b/app/dashboard.component.html new file mode 100644 index 0000000..5071720 --- /dev/null +++ b/app/dashboard.component.html @@ -0,0 +1,3 @@ +
+ {{ duck.color }} +
diff --git a/app/dashboard.component.ts b/app/dashboard.component.ts new file mode 100644 index 0000000..4bb53fa --- /dev/null +++ b/app/dashboard.component.ts @@ -0,0 +1,27 @@ +import { Component, OnInit } from "@angular/core"; +import { Router } from "@angular/router"; + +import { DuckService } from "./duck.service"; +import { Duck } from "./models"; + +@Component({ + selector: "dashboard", + templateUrl: "/app/dashboard.component.html" +}) +export class DashboardComponent implements OnInit { + ducks: Duck[] = []; + + constructor(private router: Router, + private duckService: DuckService) + {} + + ngOnInit(): void { + this.duckService.getDucks() + .then(ducks => this.ducks = ducks.slice(1, 5)); + } + + gotoDetail(duck: Duck): void { + let link = ['/detail', duck.id]; + this.router.navigate(link); + } +} diff --git a/app/duck-detail.component.html b/app/duck-detail.component.html index 2c5e6e6..201ab8e 100644 --- a/app/duck-detail.component.html +++ b/app/duck-detail.component.html @@ -1,10 +1,11 @@
- + {{ duck.id }}
- - + +
+
diff --git a/app/duck-detail.component.ts b/app/duck-detail.component.ts index 115b497..9fb700c 100644 --- a/app/duck-detail.component.ts +++ b/app/duck-detail.component.ts @@ -1,4 +1,7 @@ -import { Component, Input } from "@angular/core"; +import { Component, Input, OnInit } from "@angular/core"; +import { ActivatedRoute, Params } from "@angular/router"; + +import { DuckService } from "./duck.service"; import { Duck } from "./models"; @@ -6,7 +9,23 @@ import { Duck } from "./models"; selector: "duck-detail", templateUrl: "/app/duck-detail.component.html" }) -export class DuckDetailComponent { +export class DuckDetailComponent implements OnInit { + constructor(private duckService: DuckService, + private route: ActivatedRoute) + {} + + ngOnInit(): void { + this.route.params.forEach((params: Params) => { + let id = +params['id']; + this.duckService.getDuck(id) + .then(duck => this.duck = duck); + }); + } + + goBack(): void { + window.history.back(); + } + @Input() duck: Duck; } diff --git a/app/duck.service.ts b/app/duck.service.ts index f14ae48..b2ce01c 100644 --- a/app/duck.service.ts +++ b/app/duck.service.ts @@ -8,4 +8,8 @@ export class DuckService { getDucks(): Promise { return Promise.resolve(DUCKS); } + + getDuck(id: number): Promise { + return this.getDucks().then(ducks => ducks.find(duck => duck.id === id)); + } } diff --git a/app/ducks.component.html b/app/ducks.component.html new file mode 100644 index 0000000..e33bdc7 --- /dev/null +++ b/app/ducks.component.html @@ -0,0 +1,11 @@ +

Hello!

+ + +
+ {{ selectedDuck.color }} + +
diff --git a/index.html b/index.html index b6ba345..db56abf 100644 --- a/index.html +++ b/index.html @@ -5,6 +5,8 @@ + +