Building an interactive menu in Angular: A step-by-step guide

I am working with an Interface that looks like this:

export interface INavData {
    name?: string;
    url?: string | any[];
    icon?: string;
  
}

To populate this Interface, I use the following data structure:

export const navItems: INavData[] = [
  {
    name: 'Dashboard',
    url: '/dashboard',
    icon: 'icon-speedometer'
  },
  {
    name: 'Colors',
    url: '/Category/subcategory',
    icon: 'icon-drop'
  },
  {
    name: 'Typography',
    url: '/Category/category',
    icon: 'icon-user'
  }
}

In my database, I have a Menus table with the columns:

  1. Name
  2. Url
  3. Icon

I fetch data from this table using an API. Can anyone guide me on how to retrieve data from the API and map it to this Interface?

For context, here is an example of how I fetch data in my Service:

export class myService {
  readonly BaseURI = 'http://localhost:542213/api';
 constructor(private http: HttpClient) { }
 getMenus()
  {
    return this.http.get<Menus[]>(this.BaseURI + '/Menus');
  }
}

I am looking to dynamically extract the Name, Url, and Icon values from the API - any suggestions on how to achieve this?

Answer №1

To fetch data from an API and access it in your .ts file, you can use the subscribe method like so:

.ts

 import {myService} from 'PRTH';

 constructor(private _myService: myService)

 ngOnInit() {
   this.fetchMenuData()
 }

fetchMenuData(){
   this._myService.getMenus().subscribe((response)=>{
    console.log(response);             <-- This contains your API Data
 });
}

You should now see the API data in your console.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The template for the AppComponent is experiencing an error

Every time I run 'ng serve' command, I encounter the following error: src/app/add-dog/add-dog.component.html:8:36: 8 │ <input type="text" [(ngModel)]="formData.breed" /> ╵ ...

How can I effectively test a method within a React component using Jest and Typescript?

When working on .tsx components using Typescript and React, I want to write unit tests for the methods within my React component. For example: export default class SomeComponent extends React.Component<undefined, SomeComponentState> { someMetho ...

Unable to set a breakpoint within Angular constructor or OnInit method

I am currently facing an issue with my Angular application where breakpoints set in F12 tools in Chrome or IE are not working. I have a simple test case below: export class LoginComponent implements OnInit { message: string; constructor(private r ...

What is the method to activate a click event on an input file when a button is clicked in Angular 2?

<input type="file" accept="image/*"> <button>Upload file</button> Is there a way to activate the input type=file click event using the button's click event in Angular 2? ...

Enhancing the NextPage Typescript Type: A Step-by-Step Guide

I'm working on a NextJS dashboard with role-based access control and I need to pass an auth object to each page from the default export. Here's an image showing an example of code for the Student Dashboard Home Page: Code Example of Student Dashb ...

How will the presence of @types/react impact the higher-level files in my project?

https://i.sstatic.net/TfsLf.png https://i.sstatic.net/RqmMS.png Here is the structure of my directories vue node_modules src react_app node_modules @types/react package.json ...other file package.json Why does the presenc ...

Angular CLI integrated with Isotope version 2

I am facing difficulties when using the isotope-layout module with Angular CLI. To install the module, I used the command: npm install isotope-layout --save After installation, I added the script in my .angular-cli.json file: "scripts": [ ... " ...

Angular is unable to find any matching routes

I am currently working with two components: •QuestionsComponent •QuestionComponent Within app.module.ts, I have defined my routes using the following code: import { Component, NgModule } from '@angular/core'; import { BrowserModule } from &a ...

Cosmic - Ways to incorporate personalized validation strategies into the `getConfigValue()` function?

Why is the getConfigValue() function not retrieving validation values from custom Strategies? For example: @Injectable() export class CustomStrategy extends NbPasswordAuthStrategy { protected defaultOptions: CustomStrategyOptions = CustomStrategyOptio ...

Uninitialized variables in Angular 4.0 causing Rxjs issues

Since the update to angular 4.0, I've been encountering errors with RxJs variables. While everything builds without any issues, when I try to load the page, I receive this error message: ERROR Error: Uncaught (in promise): TypeError: Cannot read pr ...

A guide on streamlining the process of generating "this." variables within Angular

When it comes to working with Javascript, particularly Angular, I find myself using the this. syntax quite often. I'm curious to know if it's possible to concatenate variables in order to create a this. variable. For instance, is this achievab ...

Is it possible to filter an array of data using a dropdown in Angular without using a pipe?

I am facing an issue with filtering data based on the selected dropdown item. Currently, it only filters the data once when I select a filter, and after that, it always returns an empty result. Here is an example of the code: <select class="select" (c ...

Creating tests in JestJs for React components that utilize Context Provider and Hooks

As a newcomer to front-end development, I am currently working on authentication with Okta-React. To pass logged-in user information across multiple components, I'm utilizing React context with hooks. While this approach works well, I encountered an i ...

I encountered an error while trying to access an Angular 2 CLI hosted on Azure that said, "Permission denied to view this page or directory."

Currently in the process of learning angular 2, I've decided to host an app on Azure using git. Despite having no prior knowledge of git, I am following instructions from this video. This is not my first time hosting a simple app. Upon pushing my app ...

The modal functionality in AngularJS doesn't seem to be functioning properly

I am currently working on an Angular application where I want to implement a button that, when clicked, will open a pop-up window displaying a chart. Below is the button code: <div style="padding-top:50px;padding-left:10px;"> <button type="butto ...

Is there a way to focus on a specific iteration of the ngFor loop in Angular 9 using jQuery?

I'm working on a list of items inside a modal that uses *ngFor with checkboxes. The goal is to cross out the contents of an item when its checkbox is clicked. Here's the initial code using jQuery in home.component.ts: $('body').on(&apo ...

The field 'password' is not found in the 'User' array type

I'm encountering an issue with my Typescript code: Property 'password' does not exist on type 'User[]'.ts(2339). Do I need to create an interface or something similar? Thank you in advance. usersRouter.post("/", async ...

Adding Angular to your current project involves integrating the framework into your

Previously, the task of initializing a project was done using ng init, but that command no longer exists. Another option is to run ng new from a higher level in the directory structure and specify the folder for your existing project. However, this will ...

The improper utilization or replacement of Jest mock in an Angular standalone component's unit test is causing issues

Presented here is a streamlined Ionic/Angular component with unnecessary code removed. import { IonicModule, ModalController } from '@ionic/angular'; @Component({ selector: 'my-component', templateUrl: 'my-component.html' ...

Setting up a Typescript project with a shared package configuration

Before I begin, there are a few premises to consider: I require a shared package that will be included in three other packages, TypeScript, Only one node modules, Ability for multiplatform usage (Windows / Linux), To utilize dependencies from the shared ...