Skip to content

Commit 1a952e1

Browse files
committed
add routes for query-params and optional-route-parameters
1 parent 2fbd92c commit 1a952e1

File tree

6 files changed

+103
-2
lines changed

6 files changed

+103
-2
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,28 @@
22

33
This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 1.0.0-rc.1.
44

5+
## List available routes:
6+
7+
* `/`
8+
9+
* `/exam`
10+
11+
* `/exam?searchKey=xxx&page=yyy`
12+
13+
* `/exam/detail`
14+
15+
* `/exam/detail;id=xxx;step=yyy`
16+
17+
* `/product`
18+
19+
* `/product/:id`
20+
21+
* `/not-found` - any route does not match above routes.
22+
523
## Development server
24+
25+
Run `npm i` or `yarn install` to install dependecies first.
26+
627
Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files.
728

829
## Code scaffolding
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
<p>
22
exam-detail works!
33
</p>
4+
5+
<div>
6+
<input type="text" [(ngModel)]="examId">
7+
<input type="text" [(ngModel)]="step">
8+
<button (click)="doIt()">Do it</button>
9+
</div>
10+
<p *ngIf="data">
11+
<span>Exam Id: {{ data.examId }}</span>
12+
<span>Exam step: {{ data.step }}</span>
13+
</p>
Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Component, OnInit } from '@angular/core';
2+
import { Router, ActivatedRoute } from '@angular/router';
23

34
@Component({
45
selector: 'app-exam-detail',
@@ -7,9 +8,30 @@ import { Component, OnInit } from '@angular/core';
78
})
89
export class ExamDetailComponent implements OnInit {
910

10-
constructor() { }
11+
step: number;
12+
examId: number;
13+
14+
data: any = {};
15+
16+
constructor(private router: Router, private route: ActivatedRoute) { }
1117

1218
ngOnInit() {
19+
this.route.params
20+
.map(params => ({
21+
id: params['id'] || 1,
22+
step: params['step'] || 1
23+
}))
24+
.subscribe(p => {
25+
this.data.examId = p.id;
26+
this.data.step = p.step;
27+
})
28+
}
29+
doIt() {
30+
let matrixUrlData = {
31+
id: this.examId,
32+
step: this.step
33+
};
34+
this.router.navigate(['/exam', 'detail', matrixUrlData]);
1335
}
1436

1537
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
11
<p>
22
exam-list works!
33
</p>
4+
<div>
5+
<input type="text" [(ngModel)]="query" (keyup.enter)="doSearch()">
6+
<button (click)="doSearch()">Search</button>
7+
</div>
8+
<p *ngIf="searchKey">
9+
Search key: {{ searchKey }}
10+
</p>
11+
12+
<p *ngIf="page">
13+
Page: {{ page }}
14+
</p>
Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { Component, OnInit } from '@angular/core';
2+
import { Subscription } from 'rxjs/Rx';
3+
import { ActivatedRoute, Router, NavigationExtras } from '@angular/router';
24

35
@Component({
46
selector: 'app-exam-list',
@@ -7,9 +9,42 @@ import { Component, OnInit } from '@angular/core';
79
})
810
export class ExamListComponent implements OnInit {
911

10-
constructor() { }
12+
searchKey: string = '';
13+
page: number = 1;
14+
subscription: Subscription;
15+
16+
query: string = '';
17+
18+
constructor(private router: Router, private route: ActivatedRoute) { }
1119

1220
ngOnInit() {
21+
this.subscription = this.route
22+
.queryParams
23+
.map(params => ({
24+
// return object with all needed key
25+
searchKey: params['searchKey'] || '',
26+
page: params['page'] || 1
27+
}))
28+
.subscribe(param => {
29+
this.searchKey = param.searchKey;
30+
this.page = param.page;
31+
});
32+
}
33+
34+
doSearch() {
35+
let navigationExtras: NavigationExtras = {
36+
queryParams: {
37+
searchKey: this.query,
38+
page: this.page
39+
}
40+
};
41+
this.router.navigate([], navigationExtras);
42+
}
43+
44+
ngOnDestroy() {
45+
if (this.subscription) {
46+
this.subscription.unsubscribe();
47+
}
1348
}
1449

1550
}

src/app/exam/exam.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import { ExamRoutingModule } from './exam-routing.module';
55
import { ExamComponent } from './exam.component';
66
import { ExamDetailComponent } from './exam-detail/exam-detail.component';
77
import { ExamListComponent } from './exam-list/exam-list.component';
8+
import { FormsModule } from '@angular/forms';
89

910
@NgModule({
1011
imports: [
1112
CommonModule,
13+
FormsModule,
1214
ExamRoutingModule
1315
],
1416
declarations: [ExamComponent, ExamDetailComponent, ExamListComponent]

0 commit comments

Comments
 (0)