I'm currently facing an issue with my service that is responsible for making HTTP requests and returning responses. The problem arises when I try to display parts of the response on the screen within my component, as nothing seems to be showing up despite no errors being logged in the console.
Below is the code snippet of my service:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class MenuService {
constructor(private http: HttpClient) {}
menu: any;
path:'/assets/MENU.json';
getMenu(): any {
this.http.get('/assets/MENU.json').subscribe(data => {
let newValue = JSON.stringify(data).replace('{"Node":', '[');
newValue = newValue.substring(0, newValue.length - 1);
newValue += "]";
this.menu = JSON.parse(newValue);
console.log(this.menu);
return this.menu;
});
}
}
This is how my component looks like:
import { Component, OnInit } from '@angular/core';
import { MenuService } from '../menu.service';
@Component({
moduleId: module.id,
templateUrl: 'home.component.html',
styleUrls: ['home.component.css']
})
export class HomeComponent implements OnInit {
constructor(private menuService: MenuService) {}
nodes: any;
get(): void {
this.nodes = this.menuService.getMenu();
}
ngOnInit(): void {
this.get();
}
}