In Angular 4, you can easily preselect multiple options in a mat-select dropdown by passing an

Seeking assistance with setting the options of a mat-select in Angular 4.

The issue at hand is as follows:

1. Initially, there are two variables: options and checkedOptions

options: string[];
checkedOptions: string[] //Retrieved from the database; 

2. The options variable contains all available options, while the checkedOptions variable holds data fetched from the database like so:

options = ["o1", "o2", "o3", "o4", ... "oN"]
checkedOptions = ["o2", "o4"]

3. The options are displayed using the following code snippet:

<mat-form-field floatPlaceholder="always" color="accent" class="input-all">
  <mat-select multiple placeholder="{{ par.label }}">
    <mat-option *ngFor="let op of options" [value]="op">{{ op }}</mat-option>
  </mat-select>
</mat-form-field>
  1. The challenge is to display only those options in the options list that are also present in the checkedOptions list. How can this be achieved?

Your guidance on this matter would be greatly appreciated.

Answer №1

To properly handle the selection list in your mat-select-tag, make sure to incorporate ngModel. Here's how you can implement it:

HTML

<mat-form-field floatPlaceholder="always" color="accent" class="input-all">

   <mat-select [(ngModel)]="checkedOptions" multiple placeholder="{{ par.label }}">

       <mat-option *ngFor="let op of options" [value]="op">{{ op }}</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

Use Typescript in combination with React/Redux to showcase a dynamic table on the

Looking to create a React TypeScript Redux application that showcases a table using an API endpoint provided at https://example.com/users The goal is to construct a table with 4 columns: Name, Email, City, and Company, utilizing the API response to popula ...

Properly transform a set of conditions from If statements to R.cond

I have a series of If statements that I need to convert to Ramda's conditional function. However, when I try to do so, I encounter an error saying that "Never is not assigned to Element." It seems like I am making a mistake somewhere: if (cond1) ret ...

The error TS2300 arises from the duplicate identifier 'PropertyKey' found in the node_modules/@types/core-js/index.d.ts file

I am encountering errors in the file node_modules/@types/core-js/index.d.ts while using Visual Studio Code IDE: After running npm start to serve the application, the following errors are displayed: (list of errors here) This is how my tsconfig.json ...

Mapping strings bidirectionally in Typescript

I am currently working on a two-way string mapping implementation; const map = {} as MyMap; // need the correct type here const numbers = "0123456789abcdef" as const; const chars = "ghijklmnopqrstuv" as const; for (let i = 0; i < n ...

The Angular8 form encounters an error when trying to access the 'valid' property of an undefined value

I am implementing a register form to add an employee and including validation for accuracy. However, I encountered an error message that reads "cannot read property of 'valid' of undefined." Here is the code snippet: HTML <div class="conta ...

The image hover feature is not functioning as expected in Angular 4

Currently, I am involved in a project using Angular 4. One particular section involves changing images on hover. Although I have implemented the following code, it does not seem to be functioning correctly for me. Interestingly, the same code works perfect ...

Transform an Array into a String using Angular

Is there a more efficient way to extract all the state names from the array (testArray) and convert them to strings ('Arizona','Alaska','Florida','Hawaii','Gujarat','Goa','Punjab'...)? ...

Struggling with the compilation of this Typescript code

Encountering a compile error: error TS2339: Property 'waitForElementVisible' does not exist on type 'signinPage' SigninPage code snippet: export class signinPage{ constructor(){ emailInput: { selector: 'input[type ...

Sending data from an Angular 2 application to a Spring MVC Rest API using HTTP POST method

I'm encountering an issue while attempting to perform an angular 2 http post JSON with multiple attributes to Spring MVC using @RequestParam. Despite my efforts of searching for a solution, I have not been successful in binding it to my object. I even ...

Is it necessary to upload the node_modules folder to Bitbucket?

When uploading an Angular 2 app to Bitbucket, is it necessary to include the node_modules and typings folders? I am planning to deploy the app on Azure. Based on my research from different sources, it seems that when deploying on Azure, it automatically ...

Error encountered with the PrimeNG Angular2 Accordion component

https://i.sstatic.net/NqDIN.png I am currently utilizing the PrimeNG accordion module. After importing all components successfully, I encountered an issue with a newly created component. Despite verifying that all modules were imported correctly, I contin ...

Using Typescript for the factory design pattern

My goal is to develop a factory for generating instances of MainType. To achieve this, I want to reuse existing types (specifically the same instance) which are stored in the ItemFactory. class BaseType { } class MainType extends BaseType { } class It ...

Issue with mediaRecorder.ondataavailable in Angular 4 - need a solution

Currently, I am attempting to transmit real-time streaming data from an Angular 4 application to a NodeJS server. To achieve this, I have implemented the usage of socket.io and webRtc for streaming. constructor(private _chatService: ChatService) {} ngOnI ...

Tips on pairing elements from a ngFor processed list with another list using ngIf

If we have a list such as the one shown below: elements = [ { id: 1, name: "one" }, { id: 3, name: "three" }, { id: 5, name: "five" }, { id: 6, name: "six" }, ]; lists = [ { id: 5, name: "five" }, { id: 9, ...

Avoid using <input oninput="this.value = this.value.toUpperCase()" /> as it should not convert the text displayed on the "UI", rather it should send the uppercase value

<input oninput="this.value = this.value.toUpperCase()" type="text" id="roleName" name="roleName" class="form-control width200px" [(ngModel)]="role.roleName"> Even though the UI is changing ...

Issue with React/Next.js input field rejecting commas

I'm experiencing a problem with an input field in my React application that was developed using Next.js. The issue arises specifically when using an iPad device, where the input field behaves inconsistently when dealing with commas. When using deskto ...

Angular's POST request to Web.API was blocked due to cross-origin restrictions

A demonstration application is being developed at a small scale: Frontend using Angular (http://localhost:4200/) Backend using ASP.Net Core (https://localhost:44333/) Currently, the GET requests from frontend to backend are functioning correctly. Howeve ...

Exploring the capabilities of Angular 2.0.0 by testing a component integrated with a

Here's a code snippet of a component in Angular: import {Component, OnInit} from '@angular/core'; import {Route, Router} from '@angular/router'; @Component({ selector: 'app-index', templateUrl: './index.compone ...

Utilizing React Typescript to Efficiently Manage Multiple Checkboxes within a List

I'm working on a scenario where I have to manage multiple checkboxes in a list Only one checkbox can be selected at a time For example, if I toggle on Checkbox 1 and then click on Checkbox 2 - I want to automatically toggle off Checkbox 1 as I toggl ...

Aggregating all elements in an array to generate a Paypal order

I'm currently working on a React project where I need to integrate the PayPal order function and include every item from an array. Below is my code snippet, but I'm struggling with how to achieve this: export default function Cart(): JSX.Element ...