I'm facing difficulties with simple Angular/Java application. During rest call, I'm also creating a tile component. Data is being retrived - I can see logs in the network tab and in backend logs, however Tile component is not being created. Here is the code:
main.grid.ts:
@Component({
templateUrl: './main.html',
selector: 'album-grid',
})
export class MainGrid implements OnInit {
private tileDtos: TileDto [];
private album: AlbumDto [];
constructor(public dialog: MdDialog, private albumService: AlbumService, private tileUtils: TileUtils) {
this.album=[];
this.tileDtos=[];
}
ngOnInit(): void {
this.albumService.getAllAlbums().subscribe(p => this.album = p); // here is the call to rest service
console.log("@@@@@@@@@@@@@ "+ this.album.length); // this returns 0
this.tileDtos = this.tileUtils.buildTile(this.album);
console.log("@@@@@@@@@@@@@ "+ this.tileDtos.length); // this returns 0
}
}
album.service.ts:
@Injectable()
export class AlbumService {
private baseUrl = 'http://localhost:8080';
constructor(private http: Http) {
}
getAllAlbums(): Observable<AlbumDto[]> {
let getUrl = `${this.baseUrl}/getAllAlbums`;
return this.http.get(getUrl, {headers: AlbumService.getHeaders()}).map(mapAlbums);
}
private static getHeaders() {
let headers = new Headers();
headers.append('Accept', 'application/json');
return headers;
}
}
function mapAlbums(response: Response): AlbumDto[] {
let json = response.json();
let album: AlbumDto[] = json.map(toAlbum);
return album;
}
function toAlbum(r: any): AlbumDto {
let album = <AlbumDto>({
id: r.id,
albumName: r.albumName,
owner: r.owner,
images: r.images,
});
console.log("album: ",album);
return album;
}
in the logs on frontend side I can see:
@@@@@@@@@@@@@ 0
Building tile
@@@@@@@@@@@@@ 0
album: //here I'm seeing full album json populated as it should be.
Edit.
To narrow the issue, to my understanding the issue is with the order of loading in the Main.Grid.ts. as I've noticed in the logs, the rest call is being triggered after the code to create the tile element. I've tried to use hardcoded json object, and it was loaded properly.