Image error solve update
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { FormBuilder, FormGroup, FormArray, Validators } from '@angular/forms';
|
||||
import { Router } from '@angular/router';
|
||||
import { EventService } from 'src/app/service/event.service';
|
||||
import { environment } from 'src/environments/environment';
|
||||
|
||||
@Component({
|
||||
selector: 'app-event',
|
||||
@ -9,21 +9,46 @@ import { EventService } from 'src/app/service/event.service';
|
||||
styleUrls: ['./event.component.css']
|
||||
})
|
||||
export class EventComponent implements OnInit {
|
||||
events: any[] = []; // Define the type according to your model
|
||||
|
||||
events: any[] = [];
|
||||
|
||||
constructor(private eventService: EventService, private router: Router) { }
|
||||
|
||||
|
||||
ngOnInit(): void {
|
||||
this.loadEvents();
|
||||
}
|
||||
|
||||
|
||||
loadEvents(): void {
|
||||
this.eventService.getEvents().subscribe(events => this.events = events);
|
||||
this.eventService.getEvents().subscribe(events => {
|
||||
console.log('Raw events from API:', events);
|
||||
|
||||
// Convert relative URLs to absolute URLs using the backend domain
|
||||
this.events = events.map(event => ({
|
||||
...event,
|
||||
mainImage: this.getFullImageUrl(event.mainImage)
|
||||
}));
|
||||
|
||||
console.log('Processed events:', this.events);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// Convert relative URL to full URL
|
||||
private getFullImageUrl(imageUrl: string): string {
|
||||
if (!imageUrl) return '';
|
||||
|
||||
// If it's already a full URL, return as-is
|
||||
if (imageUrl.startsWith('http://') || imageUrl.startsWith('https://')) {
|
||||
return imageUrl;
|
||||
}
|
||||
|
||||
// If it's a relative URL, prepend the backend API URL
|
||||
// Remove any leading slash to avoid double slashes
|
||||
const cleanUrl = imageUrl.startsWith('/') ? imageUrl.substring(1) : imageUrl;
|
||||
return `${environment.apiUrl}/${cleanUrl}`;
|
||||
}
|
||||
|
||||
deleteEvent(id: number): void {
|
||||
if (confirm('Are you sure you want to delete this event?')) {
|
||||
// this.eventService.deleteEvent(id).subscribe(() => this.loadEvents());
|
||||
this.eventService.deleteEvent(id).subscribe(() => this.loadEvents());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user