Commit 88bfb38a by Emile TAVERNE

Ajout service et récupèration des données

parent fd88f46a
import {Component, OnInit} from '@angular/core'; import {Component, OnInit} from '@angular/core';
import {GenderEnum, PersonModel} from "../models"; import {PersonModel} from "../models";
import {DataService} from "./services/data.service";
@Component({ @Component({
selector: 'app-root', selector: 'app-root',
...@@ -11,16 +12,11 @@ export class AppComponent implements OnInit { ...@@ -11,16 +12,11 @@ export class AppComponent implements OnInit {
private persons: PersonModel[] = []; private persons: PersonModel[] = [];
ngOnInit(): void { constructor(private dataService: DataService) {
for (let i = 0; i < this.nbPerson; i++) {
const person = new PersonModel();
person.id = i;
person.lastname = 'Tata ' + person.id;
person.firstname = 'Toto';
person.email = 'toto.tata@test.fr';
person.gender = i % 2 === 0 ? GenderEnum.FEMALE : GenderEnum.MALE;
this.persons.push(person);
} }
async ngOnInit(): Promise<any> {
this.persons = await this.dataService.getPersons();
} }
} }
import {BrowserModule} from '@angular/platform-browser'; import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core'; import {NgModule} from '@angular/core';
import {HttpClientModule} from '@angular/common/http';
import {AppComponent} from './app.component'; import {AppComponent} from './app.component';
import * as Components from './components'; import * as Components from './components';
import * as Services from './services';
@NgModule({ @NgModule({
declarations: [ declarations: [
...@@ -10,9 +12,10 @@ import * as Components from './components'; ...@@ -10,9 +12,10 @@ import * as Components from './components';
Components.CardComponent Components.CardComponent
], ],
imports: [ imports: [
BrowserModule BrowserModule,
HttpClientModule
], ],
providers: [], providers: [Services.DataService],
bootstrap: [AppComponent] bootstrap: [AppComponent]
}) })
export class AppModule { } export class AppModule { }
<div class="card pam mtm tbm"> <div class="card pam mtm tbm">
<h2 class="txtcenter">{{person.firstname}} {{person.lastname | uppercase}}</h2> <h2 class="txtcenter">{{person.firstname }} {{person.lastname | uppercase}}</h2>
<img *ngIf="person.image; else elseBlock" class="center w100" [src]="person.image"> <img *ngIf="person.image; else elseBlock" class="center w100" [src]="person.image">
<ng-template #elseBlock> <ng-template #elseBlock>
<img class="center w100" [src]="defaultImages.get(person.gender)"> <img class="center w100" [src]="defaultImages.get(person.gender)">
......
import {Injectable} from "@angular/core";
import {HttpClient} from "@angular/common/http";
import {PersonModel} from "../../models";
import {environment} from '../../environments/environment';
@Injectable()
export class DataService {
constructor(private http: HttpClient) {
}
public getPersons(): Promise<PersonModel[]> {
return new Promise<PersonModel[]>((resolve, reject) => {
this.http.get(environment.api.persons).subscribe(data => {
const results: PersonModel[] = [];
data['results'].forEach((result) => {
const person = new PersonModel();
person.id = result['id']['value'];
person.firstname = result['name']['first'];
person.lastname = result['name']['last'];
person.email = result['email'];
person.image = result['picture']['large'];
results.push(person);
});
resolve(results);
}, err => {
reject(err);
});
});
}
}
export * from './data.service';
export const environment = { export const environment = {
production: true production: true,
api: {
persons: ''
}
}; };
...@@ -4,5 +4,8 @@ ...@@ -4,5 +4,8 @@
// The list of which env maps to which file can be found in `.angular-cli.json`. // The list of which env maps to which file can be found in `.angular-cli.json`.
export const environment = { export const environment = {
production: false production: false,
api: {
persons: 'https://randomuser.me/api/?results=20&seed=seed'
}
}; };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment