Tips for loading nested JSON data into an Angular Material dropdown list

My task involves extracting data from a JSON object and displaying the difficultyLevel.

Despite several attempts, I have been unable to achieve the desired outcome. What changes should be made to the HTML file?

const ELEMENT_DATA: data = {
      questions: [
        {
          difficultyLevel: 'easy',
          isOptionAvailable: true,
          isImageAvailable: false,
          imageUrl: '',
          questionType: 'e',
          maximumMarks: 20,
          timeToSolve: 12,
          questionId: 2,
          topicName: 'xs',
          classInfo: {
            classId: 10,
            className: '6',
          },
          subjectInfo: {
            subjectName: 'English',
            subjectId: 3,
          },

Interface

  interface data {
  questions: Question[];
  message: string;
  status: string;
}

interface Question {
  difficultyLevel: string;
  isOptionAvailable: boolean;
  isImageAvailable: boolean;
  imageUrl?: string;
  questionType: string;
  maximumMarks?: number;
  timeToSolve: number;
  questionId: number;
  topicName: string;
  classInfo: ClassInfo;
  subjectInfo: SubjectInfo;
  boardInfo: BoardInfo;
  images: any[];
  options: Option[];
}

What I'm trying:

    <mat-form-field class="drop-down" appearance="fill">
              <mat-label>--Difficulty Level--</mat-label>
              <mat-select [(ngModel)]="selectedValue" name="food">
                <mat-option *ngFor="let diff of  " [value]="diff.questionId">
                  {{ diff.difficultyLevel }}
                </mat-option>
              </mat-select>
            </mat-form-field>

Unfortunately, this implementation is not functioning as expected. How can I correctly specify the mat-option in this context?

Answer №1

To iterate, utilize the provided code snippet.

<mat-form-field class="drop-down" appearance="fill">
              <mat-label>--Choose Difficulty Level--</mat-label>
              <mat-select [(ngModel)]="selectedValue" name="food">
                <mat-option *ngFor="let diff of  ELEMENT_DATA.questions" [value]="diff.questionId">
                  {{ diff.difficultyLevel }}
                </mat-option>
              </mat-select>
            </mat-form-field>

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

How many characters are in SCEditor? Let's calculate and display the count

Currently, I am utilizing the real simple WYSIWYG editor SCEditor for my website. I am curious about how I can precisely determine the current number of characters in its textarea and display them below it? Although I posted a question on GitHub, it seem ...

The module '@react-navigation/native' is missing or does not have its corresponding type declarations

I'm encountering an issue with my react-native app using expo and typescript. After installing the necessary libraries via npm, I can confirm they are available in the node_modules folder (see image below). https://i.sstatic.net/6riSc.png The same pr ...

Send information through a form by utilizing a personalized React hook

I'm having difficulty understanding how to implement a hook for submitting a form using fetch. Currently, this is what I have. The component containing the form: const MyForm = (): ReactElement => { const [status, data] = useSubmitForm('h ...

Utilizing AWS Websockets with lambda triggers to bypass incoming messages and instead resend the most recent message received

I am facing an issue when invoking a lambda that sends data to clients through the websocket API. Instead of sending the actual message or payload, it only sends the last received message. For example: Lambda 1 triggers Lambda 2 with the payload "test1" ...

typescript is reporting that the variable has not been defined

interface User { id: number; name?: string; nickname?: string; } interface Info { id: number; city?: string; } type SuperUser = User & Info; let su: SuperUser; su.id = 1; console.log(su); I experimented with intersection types ...

Move two markers on Google Maps simultaneously

After researching various articles and posts on Stack Overflow about selecting multiple markers in Google Maps, I have been unable to find a solution for dragging two markers simultaneously. While I've managed to make it work so that one marker can tr ...

Updating the custom Page_ClientValidate method when utilizing an updatepanel

My web page utilizes an UpdatePanel and validation. Due to specific requirements, I need to show a custom model when validation fails. So far, the only way I've achieved this is by overriding the Page_ClientValidate client-side function: function Pag ...

Is it necessary to interact with HTML elements if the view is being generated using JavaScript?

I have a Rails application that is now integrated with react, where my views are rendered using react and JSX. I've noticed that there are some format.html responses in my code from when my views were ERB views. Now that they are not, I'm wonderi ...

Angular is encountering a situation where code is being executed before properly waiting for the response from an asynchronous

I am in need of a solution that will ensure my function waits for a response before proceeding. I want to prevent the response from being empty and ensure that my code waits for the completion of the asynchronous call. asyncReq(element: any) { this.confl ...

Issue: ALERT found in ./src/styles.scss (./node_modules/raw-loader!./node_modules/postcss-loader/lib?

Hello to all my fellow developers! I've been diving into the Angular 6 project recently and things are going smoothly for the most part. However, I keep encountering a warning in the console after running the application with (ng serve). WARNING i ...

Having trouble with Material-UI Textfield losing focus after re-rendering? Need a solution?

I am currently utilizing Material-ui Textfield to display a repeatable array object: const [sections, setSections] = useState([ { Title: "Introduction", Text: "" }, { Title: "Relationship", ...

Launching a modal within a Scala HTML list

When attempting to open a modal window within a list of items (specifically attachments), I am encountering difficulties retrieving the record ID from the list. The new modal window is opened with Scala, but I consistently receive an error stating that t ...

The error message "MongoDB encountered an issue with accessing the property 'push', as it was undefined."

Exploring node.js, express and MongoDB is a learning journey for me. While working on data association in MongoDB models, I encountered a runtime error. Even though the reference has been added to the model, the push() method fails to recognize it. Here ar ...

Are there any possible resolutions to the issues plaguing Next.Js?

After switching from React to Next.JS, I've encountered some challenges that I'm hoping to find solutions for: In contrast to React, Next.JS doesn't allow me to change a specific part of a webpage and maintain a static element like a navb ...

Click on the button to retrieve the data from the table cell

I have a table created using JavaScript <table class="table"> <thead> <tr> <th scope="col">#</th> <th scope="col">City</th> <th scope="col">Region</th> </tr> < ...

Executing a function from a JavaScript file within Robot Framework

It is possible to run javascript from a file. Run JavaScript ${CURDIR}/js_to_run.js However, I am wondering how to call a function by its name from this file? ...

Trying to understand the purpose of this Angular function has been quite puzzling for me

In my angular/node sample code, I identified a function that performs a specific task. However, I am puzzled as to why it is named differently from what I expected. By adding a console.log() statement in the code snippets, I was able to confirm that the f ...

Utilizing Filter in ReactJS to Avoid Displaying Duplicate Entries in an Array

I am facing an issue on my ReactJS page where two dropdown lists are showing duplicate values. These values are retrieved from a json file. I have tried using the filter method to eliminate duplicates, but I am unsure about how to apply it in my array whil ...

The first component triggers an event when a dynamic component is added and clicked

I currently have a component that includes a v-checkbox with dynamic behavior. The issue arises when clicking one of the dynamic v-checkbox elements within a v-for loop. This triggers the v-checkbox :key="'checkfont'" causing the checkFont prop v ...

Align Bootstrap navigation bar items at the center horizontally

I am currently working on a navigation bar that features two icons, evenly distributed. To achieve this look, I have been experimenting with scaling the icons horizontally in order to make them center-aligned within the navigation bar. However, I have not ...