Is there a way for me to retrieve a single object from an array using checkboxes?

I am facing an issue with my function that is supposed to console.log the selected object from a checkbox. However, when I select only one checkbox, it logs everything instead of returning just the value from the checkbox.

Here is the relevant HTML code:

<ng-container *ngIf="(world$ | async) as world">
  <div class="language" *ngFor="let language of world.languages">
    <mat-checkbox [checked]="get(language)">{{language.label}}</mat-checkbox>
  </div>
</ng-container>

And this is the Component snippet:

world$: Observable<World>;

get(e) {
  console.log(e);
}

You can check out the data from Firebase (world$) for more context. My question is, how can I retrieve only the object assigned to the checkbox?

- [x] English <--- When checking this checkbox it should only console.log "English" 
- [ ] French

Answer №1

It appears that you have the option of utilizing either a mat-selection-list or a mat-radio-group

Using MatSelectionList is straightforward as it allows for easy binding and retrieval of selected values. For example:

In your component template (checkboxes are automatically rendered):

<mat-selection-list #thingsList>
  <mat-list-option *ngFor="let something of things" [value]="something">
    {{something.name}}
  </mat-list-option>
</mat-selection-list>

In your component TypeScript:

@ViewChild('thingsList', { static: false }) thingsList: MatSelectionList;

To obtain the selected values, simply access:

this.thingsList.selectedOptions.selected

A demonstration on how to use this can be found in this stackblitz example. It utilizes ngModel instead of ViewChild, yielding the same result: Stackblitz Example

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

Using Typescript to pass a property as one of the keys in an object's list of values

In my React Native project, I need to pass a string value from one component to another. The different options for the value can be found in the ScannerAction object: export const ScannerAction = { move: 'move', inventory: 'inventory&apo ...

Upgrading from Angular 6 to Angular 7

After upgrading my Angular 4 app to Angular 6, I'm now looking to make the jump to Angular 7. According to a recent article, running the command below should only take around 10 minutes for the upgrade process: ng update @angular/cli @angular/core F ...

Unexpected error encountered in Angular 2 beta: IE 10 displays 'Potentially unhandled rejection [3] SyntaxError: Expected'

Question regarding Angular 2 Beta: I am starting off with a general overview in the hopes that this issue is already recognized, and I simply overlooked something during my research. Initially, when Angular 2 Beta.0 was released, I managed to run a basic m ...

Property-based Angular Material row grouping in a mat-table is a powerful feature that enhances

Is there a way to organize the data in one row with the same ID? Currently, my data looks like this: Data Set: { "id": "700", "desc": "Tempo", "richiesta": "20220087", "dataElab": &quo ...

Can you explain the purpose of the second parameter in the generic argument of a Typescript-based React component?

Exploring the use of React in combination with Typescript has been intriguing. I came across a guide on the official Typescript website that showcased a component code snippet: import * as React from "react"; export interface HelloProps { compiler: strin ...

Encountered difficulties implementing Chart.js with Angular2

Greetings! I am currently attempting to utilize one of the Charts provided by http://www.chartjs.org, however, I seem to be encountering difficulties in getting it to function properly. I have followed the documentation by installing 'npm install char ...

Ionic: Oops! Looks like we couldn't find any routes that match. URL Segment:

Currently, I'm working on developing a basic Ionic application, but I keep encountering the following error: core.js:14597 ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'match-details/match-details-info' E ...

"Convert the date format in an XLSX file to a unique

I have a function that takes a file as input and needs to convert the data to JSON format. However, I specifically want to replace the value in the column labeled DOB (05-04-2001) with a random number like 36986. The original date must still be included ...

Preventing cell editing for certain rows in PrimeNG Datatable

Thank you for taking the time to read this detailed post. I am working with an editable datatable in PrimeNG and Angular2, following a structure similar to their online demo: <p-dataTable [value]="cars" [editable]="true"> <p-column field="vin ...

Customize back button functionality in Ionic 2

Is it possible to modify the behavior of the back button shown in this image? I would like to specify a custom destination or perform an action before navigating back, instead of simply returning to the previous page. https://i.stack.imgur.com/EI2Xi.png ...

Navigating through different components in Angular2 is made simple with the

Struggling to create a single-page app and facing issues with routing. Despite following several tutorials, I find them quickly becoming outdated due to Angular2 still being in beta. Whenever any reference to router directives, configurations, or provider ...

Tips on implementing zod schema types with remapped fields using the toZod method

I'm currently working on mapping a schema key to a different name in my database interface Country { isoCode: string, nameEn: string, nameDe: string, phone: string, bla: string } const CountryJson = { i ...

What could be the reason for the Angular application image not starting up locally with Docker?

I am a beginner with Docker and I'm experimenting with running the default Angular app locally on a Docker container. After creating the app using ng new, I included the following Dockerfile: FROM node:8 RUN mkdir /usr/src/app WORKDIR /usr/src/app C ...

Explain the interaction of cookies between Angular and C# controllers

Can anyone provide clarity on how cookies are utilized between an angular application and a C# server controller? After looking through various discussions and forums, here's what I've gathered: The angular client initiates an HTTP Request (e. ...

"Users have reported that the file upload preview feature in Angular 6 only works after the

I am currently utilizing Angular 6. In my application, I have a simple input type="file" field that passes data to an image source which displays the image I intend to upload. The issue I am facing is that when I select an image for the first time, nothi ...

"X is not compatible with these types of property," but it is not the case

I attempted to instantiate an interface object with properties initialized from another object as follows: id: data.reference.id Even though the properties are compatible, the TypeScript compiler is throwing an error. I am confused about why this is happ ...

Typescript - Postpone defining generic type until invoking function

Trying to modify an existing API by defining a generic type to determine the key/value pairs that can be passed to the function, and also for IntelliSense purposes. (Working with vue.js in this case, but it's not crucial.) Here is the structure of th ...

What are some strategies for validating form fields in the Back-End and displaying them in Angular7?

My plan is to develop the backend of my app using Spring Boot and the frontend using Angular. I want to ensure the security of the form field information by validating it on the backend side. To get started, I created a model called Visitor.java with the f ...

An issue has occurred: Control with path 'flights -> 3' cannot be located

My data is structured like this "inqury" : { "id":1, "flights": "id":1, "flightChild":3, "flightChildAge":[10, 3, 9] } I attempted to use reactive forms and encountered the fo ...

Having trouble changing the query string in the URL with Angular 4?

My search form includes various filters such as text inputs, checkboxes, and radio buttons. Whenever the user interacts with these filters, the query string in the URL needs to be updated. Consider the following scenario: http://example.com/search?myFilt ...