Ways to loop through the choices within the mat-select element?

Currently, I am attempting to iterate through two arrays. One array is used to create select-fields, while the other array populates options in each select-field.


<div *ngFor="let subject of subjects" class="input-field col s6"> //looping through all subjects
   <mat-form-field>
      <mat-label>{{subject}}</mat-label> /*creating a select-field for each subject*/
         <mat-select [(value)]="selectedStaff">
             <mat-option *ngFor="let faculty of facultyNames" [value]="faculty" > /*iterating over facultyNames to fill options*/
                 {{faculty}} /*filling out the options*/
             </mat-option>
         </mat-select>
     </mat-form-field>
  </div>

The select-fields and options are being generated as intended. However, when I choose an option in one select-field, it changes the values of all other select-fields to the same choice. This doesn't allow for individual selection in each field.

As a beginner in Angular, I am struggling to find an alternative approach to solve this issue. Any guidance or suggestions would be highly appreciated. Thank you.https://i.sstatic.net/qzf6o.pnghttps://i.sstatic.net/ipOMU.png

Answer №1

Your issue stems from having the same value across all of your selections.

To fix this, update your code to:

<div *ngFor="let subject of subjects; let i = index" class="input-field col s6">

Also change:

<mat-select [(value)]="selectedStaff[i]">

Make sure that in your typescript file, the selectedStaff attribute is an array with a value for each item in subjects, or initialize it as an empty array if needed.

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

Send the index of the row to the event handler in the table of data

I am currently utilizing a data table component from PrimeNG and have the following template code: <p-column [style]="{'width':'40px'}"> <template let-col let-rowData="rowData" let-rowIndex="rowIndex" pTemplate type="body" ...

The class variable cannot access the Angular Http response returned by the service

I have a Typescript application built with Angular 2. In this application, I need to retrieve Build Artifacts from a Jenkins server using the Jenkins Rest API. The Build Artifact contains a text file that I want to read from. I am making use of Angular&apo ...

Tips for accessing the return value in Ionic 2

Having issues with getting the returned value of one function to work in another. The returned value always comes back as undefined. Here's the code I'm using for returning values: export class HomePage { variables={ a:1, b:2, c: ...

Creating a consolidated bundle of JavaScript files with the webpack module bundler for Angular application

I am new to angularjs and facing performance issues in my project due to multiple dependencies in separate js files. I want to bundle all these files into a single file using webpack. Can someone guide me on how to achieve this with webpack? ...

"I am facing an issue with the (click) event not functioning properly within a string in Angular

I have encountered an issue where my function does not get called when I click on a tag. Below is the code snippet from my component: public htmlstr: string; public idUser:number; this.idUser = 1; this.htmlstr = `<a (click)="delete(idUser)">${idUse ...

Creating synchronization mechanisms for events in JavaScript/TypeScript through the use of async/await and Promises

I have a complex, lengthy asynchronous process written in TypeScript/JavaScript that spans multiple libraries and functions. Once the data processing is complete, it triggers a function processComplete() to indicate its finish: processComplete(); // Signa ...

Angular: Display loader when navigating between different routes

I am looking to implement a preloader for when the content in the router-outlet is being loaded. The idea is to have the preloader displayed initially, covering the router-outlet, and then after a brief delay (1s), hide the preloader and reveal the conten ...

Attempting to personalize the CSS for a tab within a table in Material design is unsuccessful

Within my Angular 5 application, I have implemented the Material API. One of the components in my project is a table with 2 tabs structured like this: <mat-tab-group> <mat-tab> <mat-table> <!-- content --> </mat- ...

An error is triggered upon the refresh of a React component

Recently, I encountered an issue with a React TypeScript component that includes the following componentDidMount method: componentDidMount() { ComponentFields.get(this.ComponentName) .then(activeFields => { this.setState({ activ ...

Navigating the world of Angular 5: Essential resources for beginners

My work requires me to learn Angular 5, although Angular 7 is currently the latest version. It seems like Angular 5 may be on its way out and online resources are becoming outdated. Can anyone suggest the best and most up-to-date resources for learning Ang ...

Tips for focusing on a characteristic of a custom-made component in cypress

As a newcomer to Cypress, I'm trying to figure out how to test if clicking on my a tag changes the video in my app-video component: it('button next should show next video', function() { cy.visit('/carousel/videos'); cy.get(&ap ...

Putting VueJS, typescript, and jest to the test: examining the contents of a dropdown list web-component

Currently, I am in the process of testing a dropdown list within a web component utilized in a VueJS application. My main focus is on checking whether the dropdown list actually contains items fetched through an HTTP query (handled in a vuex store) once t ...

Ways to efficiently update the API_BASE_URL in a TypeScript Angular client generated by NSwag

Is it possible to dynamically change the API_BASE_URL set in my TypeScript client generated by NSWAG? I want to be able to utilize the same client with different API_BASE_URLs in separate Angular modules. Is this achievable? Thank you for your assistance. ...

How can we direct the user to another tab in Angular Mat Tab using a child component?

Within my Angular page, I have implemented 4 tabs using mat-tab. Each tab contains a child component that encapsulates smaller components to cater to the specific functionality of that tab. Now, I am faced with the challenge of navigating the user from a ...

Make the angular component refresh when the back button is pressed

I am working on a TeamSeasonComponent with the URL http://localhost:4200/teams/season/<team_id>. On this page, there are links to other team's pages which would take me to http://localhost:4200/teams/season/team_2_id>. I switch between these ...

Establishing limitations on the type of a value while preserving its specific type in TypeScript

Is there a way to declare a value that extends a specific type and maintains its narrow type without the need to call a function? const stringRecord : <T extends Record<string, string>>(x: T)=>T= (x) => x; //Define value that extends Re ...

Add the arrivalDate value to the existing array

Is there a way to store each arrivalDate from the API's JSON response into my array list, even though the array is currently empty? Here is a snippet of the JSON returned by the API: { "reservations": { "reservationInfo&quo ...

Is it possible to merge these two scripts into a single one within Vue?

As a newcomer to VUE, I am faced with the task of modifying some existing code. Here is my dilemma: Within a single component, there are two script tags present. One of them contains an import for useStore from the vuex library. The other script tag incl ...

The specified property is not found within this type (Union type)

When working with one of multiple interfaces, I encounter an issue in Vue where it claims the property does not exist. export interface Link { to: string text?: string; } export interface Text { text: string; } export interface Html { html: string ...

The @Hostlistener method consistently returns an 'undefined' value when passing in the @Input() value

I'm encountering an issue where the value from @Input() is undefined in my @Hostlistener method. What could be causing this? export class InputHelpComponent implements OnInit { isOpened: boolean = false; @Input() field!: string; @HostListener(& ...