[ Those who vote for close : I don't want to put everything inside ngOnit - because I told you : I need to reuse the API response and model array in many functions, so I need to write something so that I can reuse
You know, I could actually solve each problem just calling everything inside subscribe or subscribe again and again in each function.
I already tried other SO question , most of them are putting all inside ngOnIt . I want to call only necessary functions there, so don't mess that place please
Please help me to write a function so that I can reuse my API response or I can reuse the model that was Initialized by API response this.menu = data;]
I want to print a menu from my API response.
Moreover, I need to use the response multiple time in multiple functions, but I am getting null value when I am out of my subscribe block
Here is my code:
import { Component, OnInit } from '@angular/core';
// import { LoginModel } from "../../models/login/login-model";
import { MenuModel } from "../../models/menu/menu-model";
import { SubmenuModel } from "../../models/submenu/submenu-model";
// import { AccessModel } from "../../models/access/access-model";
import { MenuService } from "../../services/menu/menu.service";
import { SubmenuService } from "../../services/submenu/submenu.service";
@Component({
  selector: 'app-admin-access',
  templateUrl: './admin-access.component.html',
  styleUrls: ['./admin-access.component.css']
})
export class AdminAccessComponent implements OnInit {
  menu: MenuModel[] = null;
  submenu: SubmenuModel[] = null;
  constructor(private menuService: MenuService, private submenuSerive: SubmenuService) { }
  ngOnInit(): void {
    this.getMenu();
    this.printMenu();
  }
  getMenu() {
    this.menuService.GetAllMenu().subscribe((data: MenuModel[]) => {
      this.menu = data;
      console.log("first use : ");
      console.log(data);
      console.log("second use : ");
      console.log(this.menu);
    })
  }
  printMenu(){
    console.log("third use : ");
    console.log(this.menu);
  }
}
Here is output :
See from printMenu() function all of my response is null. But why? I did subscribe and saved the value before.
So how can I save a value from API response permanently?

 
    