Hello I have a problem with saving data in subscribe with angular 8.
when i make this this.service.getAll().subscribe(response => console.log(this.itemList = response))
and print itemList the array is empty, i try to print the response in the subscribe and response have data.
I tried to make print in subscribe and print of local array in this way :
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { ItemService } from 'src/service/item.service';
import { Item } from 'src/DTO/item';
@Component({
selector: 'app-crafting-dashboard',
templateUrl: './crafting-dashboard.component.html',
styleUrls: ['./crafting-dashboard.component.css']
})
export class CraftingDashboardComponent implements OnInit{
itemList: Item[] = [];
constructor(private router: Router, protected service: ItemService) {
}
ngOnInit() {
this.service.getAll().subscribe(response => console.log("Reponse:" + response));
console.log("Constrol print" + this.itemList);
}
getAll(){
}
}
i've noted that in console appear first the console.log("Constrol print" + this.itemList); and after the print of subscribe. It could be this the problem?!?!?
this is my service (i made an abstract service and in the specific service I add the specific method for an entity:
import { Service } from './service';
import { Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http';
export class AbstractService<DTO> implements Service<DTO> {
type: string = '';
port: string = '8080';
constructor(protected http: HttpClient){}
getAll(): Observable<DTO[]> {
return this.http.get<DTO[]>('http://localhost:' + this.port + '/' + this.type + '/getAll');
}
read(id: number): Observable<DTO> {
return this.http.get<DTO>('http://localhost:' + this.port + '/' + this.type + '/read?id=' + id);
}
insert(dto: DTO): Observable<DTO> {
return this.http.post<DTO>('http://localhost:' + this.port + '/' + this.type + '/insert', dto);
}
update(dto: DTO): Observable<DTO> {
return this.http.put<DTO>('http://localhost:' + this.port + '/' + this.type + '/update', dto);
}
delete(id: number): Observable<any> {
return this.http.delete('http://localhost:' + this.port + '/' + this.type + '/delete?id=' + id);
}
}
The back-end is written in java with spring