What could be the reason for the click event returning undefined?

I can't seem to pass a date from a mat-option choice as it returns undefined when I try to log the event in the click method. How can I solve this issue?

HTML

    <mat-drawer-content class="Font">
      <mat-form-field appearance="fill">
        <mat-label>choose the date
        </mat-label>
        <mat-select [(value)]="selected">
          <mat-option value="option1" (click)="onclick($event)">{{this.shareDate.newCurrentDate}}</mat-option>
          <mat-option value="option2" (click)="onclick($event)">{{this.shareDate.newTomorrow}}</mat-option>
        </mat-select>
      </mat-form-field>
      <mat-form-field >
        <mat-label>choose the time
        </mat-label>
        <mat-select #hasBackdrop>
          <mat-option [value]="false" *ngFor="let time of dayaHours">
            <div class="hourTime">{{formatTime(time.time)}}</div>
          </mat-option>
        </mat-select>
      </mat-form-field>
    </mat-drawer-content>
  </mat-drawer-container>

TS

onclick(event) {
  console.log(event.value); // undefined
}

Answer №1

It is not feasible to retrieve a value from a click event directly. Instead, you can try the following approach:

<mat-option #matOption1 (click)="onClick(matOption1.value)" [value]="'GB'">Great Britain</mat-option>

I have provided a Stackblitz example as well.

For further details, please refer to: https://angular.io/guide/template-reference-variables

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

Listening for changes in class property values in TypeScript with Angular involves using the `ngOnChanges`

Back in the days of AngularJS, we could easily listen for variable changes using $watch, $digest... but with the newer versions like Angular 5 and 6, this feature is no longer available. In the current version of Angular, handling variable changes has bec ...

What are the best practices for sharing context in express and typescript?

Implementing a solution to expose a value to all request handlers using express and typescript is my goal. I am looking for a way to easily "inject" this value from a middleware or an alternative method that allows for simple mocking if needed. Here is th ...

TypeScript class-module declaration file

I am encountering an issue with a node module that is not recognized by typings and is not available in definelytyped. The basic usage of this module is shown below: import * as SomeClass from 'some-module'; var someObject = new SomeClass("som ...

Issue found in Next.js 14: Hydration Error | Caution: Client Components can only receive plain objects from Server Components

I'm currently in the process of creating an application using Next.js 14, TypeScript, Mongoose, and MongoDB. In this app, I retrieved user data from my database and displayed them on cards along with relevant information like tags represented by badg ...

What is the best way to define a general class within the constructor of another class in TypeScript?

Is there a way to inject a subclass into a class during its constructor using TypeScript? I've tried to demonstrate my idea with some pseudo-code here: type GenericConstructor<T> = { new (): T; } class MyClass { constructor( SubClass: G ...

Utilizing TypeScript Types for Supabase Integration

I am attempting to incorporate Supabase typescript types into my Next.js project, however, I encounter the error Expected 0 type arguments, but received 1. I'm following the instructions outlined in this documentation. Although I've successfully ...

What is the best way to extract items from another array that have approved IDs - is it through using map(), filter(),

I am unsure about the best approach to retrieve only the items (array with objects) that have been approved based on their id. Should I start by using map() and then filter(), or should I filter() them first and then map()? export class AppComponent { ...

Unable to create resource in nestjs due to typeScript compatibility issue

Encountered an Error: TypeError: Cannot access 'properties' property of undefined Failed to execute command: node @nestjs/schematics:resource --name=post --no-dry-run --language="ts" --sourceRoot="src" --spec Attempts made ...

Creating a custom type for the parameter of an arrow function in Typescript

I need assistance defining the type for an object parameter in an arrow function in TypeScript. I am new to TypeScript and have not been able to find any examples illustrating this scenario. Here is my code: const audioElem = Array.from(videoElem.pare ...

NextJS is currently unable to identify and interpret TypeScript files

I am looking to build my website using TypeScript instead of JavaScript. I followed the NextJS official guide for installing TS from scratch, but when I execute npm run dev, a 404 Error page greets me. Okay, below is my tsconfig.json: { "compilerOption ...

Incorporating XMLHttpRequest in my React Typescript App to trigger a Mailchimp API call, allowing users to easily sign up for the newsletter

My website needs to integrate Mailchimp's API in order for users to subscribe to a newsletter by entering their email into a field. I am looking to implement this without relying on any external libraries. To test out the functionality, I have set up ...

How can we pass the onClick prop from a child component to a parent component in React with Typescript?

Currently, I am utilizing React along with TypeScript. I am curious about the process of passing an event from the parent component to a child component using props. Here is an example for better understanding: parent.tsx const ParentComponent: React.F ...

What is the best way to remove offline-hosted Angular Material icons?

I've set up Angular material icons for offline use, and I'm only using a few of them. Is there a method to remove the unused icons and their corresponding CSS classes to save space? ...

I am experiencing an issue in Angular 6 where the close button is unresponsive when I add a carousel within a

I successfully added a carousel in a modal popup and it is working properly. However, I encountered an issue when trying to close the modal using the close button - it is not functioning. Here is a link to the stackblitz demo for reference. You can also ...

Obtain the count of unique key-value pairs represented in an object

I received this response from the server: https://i.stack.imgur.com/TvpTP.png My goal is to obtain the unique key along with its occurrence count in the following format: 0:{"name":"physics 1","count":2} 1:{"name":"chem 1","count":6} I have already rev ...

When attempting to instantiate a new file handler with the "new" keyword, the error "filehandler is not a constructor" is

Encountering the issue of "filehandler is not a constructor" when trying to use "new filehandler", but it does not work as a static class. USAGE: demos.filehandler.mjs file: import { default as filehandler } from "../index.js"; const FileHandl ...

Error: Exceeded Maximum Re-Renders. React has set a limit on the number of renders to avoid infinite loops. Issue found in the Toggle Component of Next.js

I am struggling with setting a component to only display when the user wants to edit the content of an entry, and encountering an error mentioned in the title. To achieve this, I have utilized setState to manage a boolean using toggle and setToggle, then ...

Encountering a "No exported member" error while attempting to include & { session: Express.Session } in the MyContext type while following a tutorial on React, Express, and Typescript

Currently exploring a React, Express, and Typescript tutorial, which is still quite new to me. I am trying to grasp the concept of setting up custom types, and this is what I have so far. In my types.ts file: import { Request, Response } from "expres ...

The Angular service not only stores data but also handles HTTP requests internally

Instead of returning an observable to the requesting components and managing the data, I am considering using Angular services to handle HTTP requests autonomously. The goal is to have components retrieve data directly from the service where it is stored. ...

What is the best way to access a private class variable within the sockent.on function in Angular?

export class AppComponent { title = 'my-app'; constructor(private notifyService : NotificationService) {} ngOnInit() { socket.on("laravel_database_chat:test", function(message){ //I AM ATTEMPTING TO INVOKE THE NOTIF ...