Valentin Kononov
Full Stack Developer, Speaker
learn how to use HttpClient to fetch and alter the data
import { HttpClientModule } from '@angular/common/http';
@NgModule({
declarations: [],
imports: [
HttpClientModule,
...
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
import { HttpClient } from '@angular/common/http';
constructor(
private httpClient: HttpClient,
) { }
...
getVideos(): Observable<any> {
return this.httpClient.get("http://localhost:3000/video");
}
setVideo(data: any): Observable<any> {
return this.httpClient.post("http://localhost:3000/video", data);
}
export const environment = {
production: false,
apiUrl: ' http://localhost:3000',
};
export interface VideoModel {
id: string;
title: string;
views: number;
likes: number;
dislikes: number;
}
@Injectable({
providedIn: 'root',
})
export class AppService {
constructor(private httpClient: HttpClient) { }
getVideos(): Observable<any> {
return this.httpClient.get(`${environment.apiUrl}/video`);
}
}
@Injectable({
providedIn: 'root',
})
export class AppService {
constructor(private httpClient: HttpClient) { }
getVideos(): Observable<VideoModel[]> {
return this.httpClient.get<VideoModel[]>(
`${environment.apiUrl}/video`)
}
}
@Injectable({
providedIn: 'root',
})
export class AppService {
constructor(private httpClient: HttpClient) { }
getVideoIds(): Observable<string[]> {
return this.httpClient.get<VideoModel[]>(
`${environment.apiUrl}/video`)
.pipe(map((data: any[]) => {
return data.map(x => x.id);
}));
}
}
@Injectable({
providedIn: 'root',
})
export class AppService {
constructor(private httpClient: HttpClient) { }
getVideoById(id: string): Observable<VideoModel> {
return this.httpClient.get<VideoModel>(
`${environment.apiUrl}/video/${id}`);
}
}
@Injectable({
providedIn: 'root',
})
export class AppService {
constructor(private httpClient: HttpClient) { }
getVideoById(param1: number, param2: string): Observable<VideoModel> {
return this.httpClient.get<VideoModel>(
`${environment.apiUrl}/video/?param1=${param1}¶m2=${param2}`);
}
}
@Injectable({
providedIn: 'root',
})
export class AppService {
constructor(private httpClient: HttpClient) { }
getWithParams(param1: number, param2: string): Observable<VideoModel[]> {
const params = new HttpParams();
params.set('param1', param1.toString())
params.set('param2', param2)
return this.httpClient.get<VideoModel[]>(
`${environment.apiUrl}/video`, { params });
}
}
options?: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
observe?: 'body';
params?: HttpParams | {
[param: string]: string | string[];
};
reportProgress?: boolean;
responseType?: 'json';
withCredentials?: boolean;
}
@Injectable({
providedIn: 'root',
})
export class AppService {
constructor(private httpClient: HttpClient) { }
getVideos(): Observable<VideoModel[]> {
return this.httpClient.get<VideoModel[]>(`${environment.apiUrl}/video`)
.pipe(
retry(3),
catchError((error: any, caught: Observable<VideoModel[]>) => {
console.log(error);
return caught;
})
);
}
}
// run server
// open other terminal / cmd
cd server
npm start
By Valentin Kononov