Skip to content

Commit f0df5c6

Browse files
authored
Angular optional route
1 parent 4eccc4c commit f0df5c6

21 files changed

+55
-15
lines changed

ASP.NET Core Basics/src/Angular/ClientApp/app/app.module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { CounterComponent } from './components/counter/counter.component';
2828
{ path: 'counter', component: CounterComponent },
2929
{ path: 'fetch-data', component: FetchDataComponent },
3030
{ path: 'contact-list', component: ContactListComponent },
31+
{ path: 'contact-detail', component: ContactDetailComponent },
3132
{ path: 'contact-detail/:id', component: ContactDetailComponent },
3233
{ path: '**', redirectTo: 'home' }
3334
])

ASP.NET Core Basics/src/Angular/ClientApp/app/components/contacts/contactdetail.component.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@
1414
<dd>{{contact.email}}</dd>
1515
</dl>
1616
</div>
17+
<h3 *ngIf="!hasContactId">
18+
Place holder for creating a new contact
19+
</h3>
1720
<a routerLink="/contact-list">Back to List</a>
1821
<hr />

ASP.NET Core Basics/src/Angular/ClientApp/app/components/contacts/contactdetail.component.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,22 @@ import { ContactService } from './contact.service';
1111
})
1212
export class ContactDetailComponent implements OnInit {
1313
contact: Contact;
14+
hasContactId: boolean;
1415

1516
constructor(private route: ActivatedRoute,
1617
private router: Router,
1718
private contactService: ContactService) { }
1819

1920
ngOnInit(): void {
21+
var contactId: string;
22+
2023
this.route.params
21-
.switchMap((params: Params) => this.contactService.getById(params['id']))
22-
.subscribe((contact :Contact) => this.contact = contact);
24+
.subscribe((params: Params) => contactId = params['id']);
25+
this.hasContactId = contactId != undefined;
26+
27+
if (this.hasContactId) {
28+
this.contactService.getById(contactId)
29+
.then((contact: Contact) => this.contact = contact);
30+
}
2331
}
2432
}

ASP.NET Core Basics/src/Angular/ClientApp/app/components/contacts/contactlist.component.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
<p *ngIf="!contacts"><em>Loading...</em></p>
44

5+
<a [routerLink]="['/contact-detail']">Create New Contact</a>
6+
57
<table class="table" *ngIf="contacts">
68
<thead>
79
<tr>

ASP.NET Core Basics/src/Angular/ClientApp/dist/main-server.js

Lines changed: 11 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ASP.NET Core Basics/src/Angular/wwwroot/dist/0.08910374996c2da56ee7.hot-update.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ASP.NET Core Basics/src/Angular/wwwroot/dist/0.1e4c38dcdaeb1acdc7a5.hot-update.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ASP.NET Core Basics/src/Angular/wwwroot/dist/0.3776206e0899ae567aa0.hot-update.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)