I am looking to store the service response in a variable for use in my view. The TypeScript file I am working with is shown below:
The MenuService is a custom service that includes a function called getMenus() to fetch all menus from the database.
import { Component, OnInit } from '@angular/core';
import { TranslateService } from '@ngx-translate/core';
import { MenusService } from '../menus.service';
@Component({
selector: 'app-header1',
templateUrl: './header1.component.html',
styleUrls: ['./header1.component.css'],
providers:[MenusService]
})
export class Header1Component implements OnInit {
menus=['Login','Register','Subscribe'];
primeryMenus:any;
//menus1=['Home','Matches','Players','Teams','Tournaments','Contact Us','About Us'];
constructor(private translate: TranslateService,private _ser:MenusService) {
translate.setDefaultLang('en');
}
ngOnInit(){
this.getMenu();
}
getMenu(){
this._ser.getMenus().subscribe(res=>{
this.primeryMenus = res;
console.log(this.primeryMenus) // output is json object ( getting correct output )
});
console.log(this.primeryMenus) // output is undefined
}
switchLanguage(language: string) {
this.translate.use(language);
}
}
I need help on how to assign the response data to the `primeryMenus` variable within the subscribe method of the observable.