Skip to content

Commit 34c20d3

Browse files
authored
Create contact detail in Angular. (#5)
* Add files and part of the implementation for contact detail in the Angular project. * Complete contact detail changes for Angular project.
1 parent a99031e commit 34c20d3

12 files changed

+386
-24
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { NavMenuComponent } from './components/navmenu/navmenu.component';
66
import { HomeComponent } from './components/home/home.component';
77
import { FetchDataComponent } from './components/fetchdata/fetchdata.component';
88
import { ContactListComponent } from './components/contacts/contactlist.component';
9+
import { ContactDetailComponent } from './components/contacts/contactdetail.component';
910
import { CounterComponent } from './components/counter/counter.component';
1011

1112
@NgModule({
@@ -16,6 +17,7 @@ import { CounterComponent } from './components/counter/counter.component';
1617
CounterComponent,
1718
FetchDataComponent,
1819
ContactListComponent,
20+
ContactDetailComponent,
1921
HomeComponent
2022
],
2123
imports: [
@@ -26,6 +28,7 @@ import { CounterComponent } from './components/counter/counter.component';
2628
{ path: 'counter', component: CounterComponent },
2729
{ path: 'fetch-data', component: FetchDataComponent },
2830
{ path: 'contact-list', component: ContactListComponent },
31+
{ path: 'contact-detail/:id', component: ContactDetailComponent },
2932
{ path: '**', redirectTo: 'home' }
3033
])
3134
]

ASP.NET Core Basics/src/Angular/ClientApp/app/components/contacts/contact.service.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,24 @@ import { Contact } from './contact';
55

66
@Injectable()
77
export class ContactService {
8+
baseUrl = 'http://localhost:13322/api/contactsApi/';
89

910
constructor(private http: Http) {
1011
}
1112

1213
getAll(): Promise<Contact[]> {
13-
return this.http.get('http://localhost:13322/api/contactsApi/')
14+
return this.http.get(this.baseUrl)
1415
.toPromise()
1516
.then(response => response.json())
1617
.then(contacts => Array.from(contacts, c => new Contact(c)))
1718
.catch(error => console.log(error));
1819
}
20+
21+
getById(id: string): Promise<Contact> {
22+
return this.http.get(this.baseUrl + id)
23+
.toPromise()
24+
.then(response => response.json())
25+
.then(contact => new Contact(contact))
26+
.catch(error => console.log(error));
27+
}
1928
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<h1>Contact Details</h1>
2+
<hr />
3+
<div *ngIf="contact">
4+
<dl class="dl-horizontal">
5+
<dt>ID</dt>
6+
<dd>{{contact.id}}</dd>
7+
<dt>Name</dt>
8+
<dd>{{contact.name}}</dd>
9+
<dt>Address</dt>
10+
<dd>{{contact.getAddress()}}</dd>
11+
<dt>Phone</dt>
12+
<dd>{{contact.phone}}</dd>
13+
<dt>Email</dt>
14+
<dd>{{contact.email}}</dd>
15+
</dl>
16+
</div>
17+
<a routerLink="/contact-list">Back to List</a>
18+
<hr />
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Component, OnInit } from '@angular/core';
2+
import { Router, ActivatedRoute, Params } from '@angular/router';
3+
import 'rxjs/add/operator/switchMap';
4+
import { Contact } from './contact';
5+
import { ContactService } from './contact.service';
6+
7+
@Component({
8+
selector: 'contactdetail',
9+
template: require('./contactdetail.component.html'),
10+
providers: [ContactService]
11+
})
12+
export class ContactDetailComponent implements OnInit {
13+
contact: Contact;
14+
15+
constructor(private route: ActivatedRoute,
16+
private router: Router,
17+
private contactService: ContactService) { }
18+
19+
ngOnInit(): void {
20+
this.route.params
21+
.switchMap((params: Params) => this.contactService.getById(params['id']))
22+
.subscribe((contact :Contact) => this.contact = contact);
23+
}
24+
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,14 @@
77
<tr>
88
<th>ID</th>
99
<th>Name</th>
10-
<th>Address</th>
11-
<th>Phone</th>
12-
<th>Email</th>
10+
<th></th>
1311
</tr>
1412
</thead>
1513
<tbody>
1614
<tr *ngFor="let contact of contacts">
1715
<td>{{contact.id}}</td>
1816
<td>{{contact.name}}</td>
19-
<td>{{contact.getAddress()}}</td>
20-
<td>{{contact.phone}}</td>
21-
<td>{{contact.email}}</td>
17+
<td><a [routerLink]="['/contact-detail', contact.id]" (click)="onSelect(contact)">Details</a></td>
2218
</tr>
2319
</tbody>
2420
</table>

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@ import { ContactService } from './contact.service';
99
})
1010
export class ContactListComponent implements OnInit {
1111
contacts: Contact[];
12+
selectedContactId: number = null;
1213

1314
constructor(private contactService: ContactService) { }
1415

1516
ngOnInit(): void {
1617
this.contactService.getAll()
1718
.then(contacts => this.contacts = contacts);
1819
}
20+
21+
onSelect(contact) {
22+
this.selectedContactId = contact.id;
23+
}
1924
}

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

Lines changed: 78 additions & 6 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.22c82f1161e5b9b1af4a.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.d56cd835c3bbc94f1b20.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.edaedb0bc344cc345e98.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)