I am new in Angular. I have issue in mapping. I called Web API in Angular following is the JSON object coming from the backend:
{
"productCategoryDataModel": [
{
"productsCategortyName": "Electronics",
"productsList": [
{
"id": 1,
"productName": "Laptop",
"productDescription": "Laptop Core i7",
"productImageUrl": "fsdgdfgdfgdfgd"
},
{
"id": 5,
"productName": "IPad",
"productDescription": "IPad",
"productImageUrl": "hgfhgfhgf"
}
]
},
{
"productsCategortyName": "Grocery",
"productsList": [
{
"id": 3,
"productName": "Tomato",
"productDescription": "Tomato",
"productImageUrl": "dgdfgdfggdfgdf"
},
{
"id": 4,
"productName": "Onion",
"productDescription": "Onion",
"productImageUrl": "hgfhgfgh"
}
]
}
]
}
Following is the Response model class of Web API:
public class ProductsResponseModel
{
public List<ProductCategoryDataModel> ProductCategoryDataModel { get; set; }
}
public class ProductCategoryDataModel
{
public string? ProductsCategortyName { get; set; }
public List<ProductsList> ProductsList { get; set; }
}
public class ProductsList
{
public int Id { get; set; }
public string? ProductName { get; set; }
public string? ProductDescription { get; set; }
public string? ProductImageUrl { get; set; }
}
I have created the following interface in angular for this JSON:
export interface IProductsResponse {
ProductCategoryDataModel: ProductCategoryDataModel[];
}
export interface ProductCategoryDataModel
{
ProductsCategortyName: string
productsList: ProductsList[];
}
export interface ProductsList
{
ProductId: number;
ProductName: string;
ProductDescription: string;
ProductImageUrl: string;
}
Following is my Service class that calling the API:
@Injectable({
providedIn: 'root'
})
export class ProductsListService {
apiUrl: string = 'https://localhost:7025/api/ProductsManagement/GetAllProducts';
constructor(private httpClient: HttpClient) { }
getProductsGalleryData(): Observable<IProductsResponse[]> {
return this.httpClient.get<IProductsResponse[]>(this.apiUrl);
}
Following is my component ts file:
@Component({
selector: 'app-products-gallery',
templateUrl: './products-gallery.component.html',
styleUrls: ['./products-gallery.component.css']
})
export class ProductsGalleryComponent implements OnInit {
productsList: IProductsResponse[] | undefined;
constructor(private _galleryProducts: ProductsListService) { }
ngOnInit(): void {
this._galleryProducts.getProductsGalleryData().subscribe(data => {
this.productsList = data;
console.log(data);
});
}
}
The data is displayed in the Console in the form of JSON but I have issue in the HTML file because when I use the ngFor loop for ProductsList the properties are not coming there and it gives me error so how to map the response into the interface and how to write the html to display this data?
products-gallery.component.htmlthat makes use of the typescript interface model. This can show us why you might be getting an error especially when combined with the json I mentioned earlier. You need to include the error itself which includes the error message and the stack trace. You can get this from the browser development console. Also include the c# model being sent.