I have this service that gets a list of objects from a django rest api.
import {Injectable} from '@angular/core';
import {Headers, Http} from '@angular/http';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/map';
import {environment} from '../../../environments/environment';
import {Delegation} from "../delegations/delegation";
@Injectable()
export class DelegationsService {
private headers = new Headers({'Content-Type': 'application/json'});
constructor(private http: Http) { }
getDelegations(userid: number) {
return this.http.get(environment.apiUrl + '/api/v1/delegations/' + userid )
.map(res => <Delegation[]>res.json());
}
}
The contents of ../delegations/delegation.ts
is:
export class Delegation {
id: number;
delegator: number;
delegatee: number;
}
The numbers for the delegator and delegatee are the database id for "Users". I need to display the actual users' names and login ids which are available via another REST API url. I thought I could modify the ../delegations/delegation.ts
to be something like this:
export class Delegation {
id: number;
delegator: number;
delegator_login: string;
delegator_name: string;
delegatee: number;
delegatee_login: string;
delegatee_name: string;
}
Then I would iterate over elements I get from this.http.get(environment.apiUrl + '/api/v1/delegations/' + userid )
call and make a request for the user info, but I have no clue how to do iterate of the result set.
UPDATE:
Currently I can dump the delegator data to my delegations.component.html template and web page displays:
2 | 97597 | 5197
4 | 97597 | 9757
What I want to display is:
2 | Sam Smith (ssmith) | Lisa Butts (lbutts)
4 | Sam Smith (ssmith) | Bill Fatlips (bfatlips)
My delegations.component.html
is very simple:
<ul class="delegations">
<li *ngFor="let delegation of delegations" [class.selected]="delegation === selectedDelegation" (click)="onSelect(delegation)">
<span class="badge">{{delegation.id}}</span> | {{delegation.delegator}} | {{delegation.delegatee}}
</li>
</ul>
It is pretty much what they have in the Heroes Angular tut: https://angular.io/tutorial/