I'm new in Angular, I am making an app in Angular 5 to show images with information relative to the image, the app has a thumbnails page an when you click you can see the image with a description or a video, I'm fetching all the data from a service and an array inside the service but I need to change and instead of fetch the data from the array I need to do it from a JSON but keeping all the functionality I already have, but when I fetch the data from the JSOM I just cant see the information of image when I click.
This is the service with the array I need to change for JSON:
export class ArticulosService {
enter code here @Injectable()
export class ArticulosService {
constructor(private http: HttpClient) {}
visibleImages = [];
private url: string = '../assets/data/articulos.json';
getImages() {
return this.visibleImages = IMAGES.slice(0);
}
getImage(id: number) {
return IMAGES.slice(0).find(image => image.id === id);
}
}
const IMAGES = [
{..this is the array..} ];
This is the component with the image thumbnails here you can see all the thumbnails from the IMAGES array
export class HomeComponent implements OnChanges {
images: any[];
visibleImages: any[] = [];
constructor(private articulosService: ArticulosService) {
this.visibleImages = this.articulosService.getImages();
}
ngOnChanges() {
this.visibleImages = this.articulosService.getImages();
}
}
This is the image page component where you see the image title etc..
export class TutorialPostsComponent implements OnInit {
image:any
visibleImages: any[] = [];
constructor(private articulosService: ArticulosService, private route:
ActivatedRoute) {
this.visibleImages = this.articulosService.getImages();
}
ngOnInit() {
this.image = this.articulosService.getImage(
+this.route.snapshot.params['id']
);
}
}
Here I changed the service to fetch from a JSON instead of the hardcoded array
export class ArticulosService {
private _url: string = '../assets/data/articulos.json';
constructor(private _http: HttpClient) {}
getImages(): Observable<TutoGaleria[]> {
return this._http.get<TutoGaleria[]>(this._url)
}
}
I can see the data in the thumbnails but this data is not passing for the image details as when I was using the array.
I think is related to the getImage(id: number) function I don't know how to do this part fetching from a JSON.
How can I do the same functionality with the JSON?