What is the best way to retrieve entire (selected) objects from a multiselect feature in Angular?

I'm facing an issue with extracting entire objects from a multiselect dropdown that I have included in my angular template.

Although I am able to successfully retrieve IDs, I am struggling to fetch the complete object. Instead, in the console, it displays [object Object].

<!-- Select All option -->
<div class="form-group row">
<label class="col-form-label col-lg-3">Roles:</label>
<div class="col-lg-9">
  <select id="applicationModuleFormSelect" name="applicationModuleFormSelect"  [(ngModel)]="amf" class="form-control multiselect-select-all" multiple="multiple" data-fouc>
    <option *ngFor="let amf of appModuleForms;" [value]="amf">{{amf.title}}</option>
  </select>
</div>
</div>
<!-- /select All option -->

 onSubmit() {
    var selectedAppForms = $('#applicationModuleFormSelect').val();
    console.log((selectedAppForms));
}

It is evident in the code where I have written [value]="amf", but unfortunately, the output in the console looks like this:

[![enter image description here][1]][1]

Even when applying JSON.stringify, the results are consistent:

["1: Object","3: Object"]

Any help or suggestions are greatly appreciated. Thanks in advance!

Answer №1

Utilize [(ngModel)]="selectedData" for this purpose

Check out this Stackblitz Demo

<div class="form-group row">
<label class="col-form-label col-lg-3">Roles:</label>
<div class="col-lg-9">
  <select id="applicationModuleFormSelect" name="applicationModuleFormSelect"  [(ngModel)]="selectedData" class="form-control multiselect-select-all" multiple="multiple" data-fouc>
    <option *ngFor="let amf of appModuleForms;" [value]="amf">{{amf.title}}</option>
  </select>
</div>
</div>

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

Image not being sent to the back-end server

I've successfully set up the back-end to retrieve files using the post method in Postman. However, I'm facing an issue with the Angular2 front-end as the service code seems to be incorrect. data1 contains the image data. addData(postData: Imag ...

Issue encountered: Jest database test does not end when using testcontainers

I am currently in the process of testing my database within my Node application written in Typescript. I have implemented Postgres 15 and Testcontainers for this purpose. Strangely, my code functions correctly when executed manually, with all clients being ...

Typescript enables bidirectional control of Swiper

I attempted to use the two-way control slider example from Swiper documentation, but I encountered TypeScript errors that prevented it from working correctly. Is there a way to make it compatible with TypeScript? The specific errors I received were: TS23 ...

There seems to be an issue with the type error regarding the return of the mysql2/promise

As I delve into using the mysql2/promise library with Typescript, I've encountered a puzzling issue regarding the return type of the query method. Despite my best efforts, I can't seem to resolve an error in my code. Here is a snippet from my c ...

Replace Nebular theme with a new design for a single component

I am currently using Nebular in my Angular application and I am trying to figure out how to customize the theme settings for a specific component on just one page, without affecting other instances of the same component that are using the default settings. ...

Ignoring TypeScript type errors in ts-jest is possible

Recently, I embarked on a journey to learn TypeScript and decided to test my skills by creating a simple app with jest unit testing (using ts-jest): Here is the code snippet for the 'simple app.ts' module: function greet(person: string): string ...

The TypeScript error message states, "The property 'breadcrumb' is not found within the type 'Data'."

My Breadcrumb Component is functioning properly when compiled to JavaScript and displaying the desired output. However, my IDE is showing an error message that I am struggling to fix. The error states: [ts] Property 'breadcrumb' does not exist o ...

Clicked but nothing happened - what's wrong with the function?

For my project, I have incorporated tabs from Angular Material. You can find more information about these tabs here. Below is the code snippet I am using: <mat-tab-group animationDuration="0ms" > <mat-tab></mat-tab> < ...

The EventEmitter in Angular 8 is prohibiting the emission of an Object

Is there a way I can emit an Object instead of primitive data types? I have an object const amount = { currenty: 'USD', total: '10.25' }; that I need to emit to the parent component. export class MyChildComponent implements OnInit { ...

Retrieving data using Angular 2's HTTP GET method

I am struggling to properly handle the success and failure responses from my API. When I receive a response in literal form, it gives me an error in the controller. Below is my service code: authentication(userName : string, password:string){ return ...

Transform Observable RxJS into practical results

In a straightforward scenario, I have an upload action on the page that looks like this: onUpload$: Subject<SomeUpload> = new Subject<SomeUpload>(); uploadAction$: Observable<Action> = this.onUpload$.map(entity => this.someActionServi ...

Having trouble receiving error messages in Angular 2

In my AuthService, an error message is being thrown as a Business Error. When this error is caught in my LogInComponent, instead of displaying the error message "Login failed Error", it shows "Type object Object" in the console log. Why is this happening a ...

Guide on populating text boxes in a form automatically using ngFor?

As a beginner Angular developer, I am looking to automatically fill in text boxes in a form, specifically 10 text boxes (10 rows) using the ngFor directive. In my research on ngFor examples, I have noticed that most of them focus on populating a list base ...

Issue: Unable to locate a differ that supports the object '[object Object]' of type 'object'. NgFor can only bind to Iterables like Arrays

I have successfully pulled data from the jsonplaceholder fake API and now I am attempting to bind it using Angular 2 {{}} syntax. However, I encountered an error that states: "Error: Cannot find a differ supporting object '[object Object]' of typ ...

When deciding between StoreModule.forRoot and StoreModule.forFeature, consider the specific needs of your application

After researching, I discovered that the .forRoot function merges all reducers, allowing you to manipulate multiple reducers simultaneously when manipulating the state. On the other hand, the .forFeature function gives you the ability to manipulate each r ...

Steering clear of Endless Loops in Spring Boot

When utilizing a Spring Boot Rest API, I successfully prevented an infinite loop by using @JsonIgnore. However, in the postman response, the related list on the many side appears as null. How can I go about displaying this related list in Angular even if i ...

Retrieve the values stored under the "kilos" key for every object in the dataset

Recently, I stumbled upon the following code snippet: data = [ 0: {kilos: 10}, 1: {other:1, kilos: 5}, 2: {other:2, kilos:6} ] After analyzing it, I realized that I need to extract all the values associated with the "kilos" keys and then calculat ...

Step-by-step guide on displaying SVG text on a DOM element using Angular 8

I have a FusionChart graph that I need to extract the image from and display it on the same HTML page when the user clicks on the "Get SVG String" button. I am able to retrieve the SVG text using this.chart.getSVGString() method, but I'm unsure of ho ...

Angular 10: Customize Paypal button to open a new window when selecting "Pay by debit or credit card" option

In my Angular 10 app, I have successfully implemented a Paypal button. However, there is an issue where the "Paypal" button opens a new window while the "Pay by debit or credit card" option opens an inline form. This behavior mirrors what is seen at https: ...

Having trouble getting Next.js 404 page to function properly with the .tsx extension?

My latest project involved creating a Next.js application using regular JavaScript, which led to the development of my 404 page. 404.js import { useEffect } from "react"; import { useRouter } from "next/router"; import Link from " ...