What is the process for resetting the mat-date-range-input selection on the calendar?

I've encountered a puzzling problem that has me stumped. I'm working with a mat date range picker in Angular Typescript and have run into an issue while trying to clear any selection made through a function.

The code snippet below successfully clears the selected date(s) as confirmed by console.log, but strangely, when I reopen the calendar after clearing, the previously selected dates still remain highlighted.

HTML

  <mat-form-field class="datePickerForm">
                <mat-date-range-input [formGroup]="range" [rangePicker]="picker" [disabled]="isDateRangeDisabled" #dateRangeInput>
                    <input matStartDate formControlName="start" placeholder="Select date range" (dateChange)="onDateFilter($event)" #startDate>
                    <input matEndDate formControlName="end" placeholder="" (dateChange)="onDateFilter($event)" #endDate>
                  </mat-date-range-input>
                  <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
                  <mat-date-range-picker #picker></mat-date-range-picker>
            </mat-form-field>

TS Clear function

@ViewChild('startDate') startDateRef: ElementRef;
@ViewChild('endDate') endDateRef: ElementRef;

 range = new FormGroup({
         start: new FormControl(),
         end: new FormControl(),
 });

...
this.startDate = undefined;
this.endDate = undefined;
this.startDateRef.nativeElement.value = '';
this.endDateRef.nativeElement.value = '';
this.range.value.start = null;
this.range.value.end = null;
...

For reference, onDateFilter() change event code

onDateFilter(event) {
this.startDate = moment(this.range.value.start).format('YYYY-MM-DDT00:00:00.000') + 'Z'
this.endDate = moment(this.range.value.end).format('YYYY-MM-DDT23:59:59.000') + 'Z'

this.validateDateRange(this.startDate, this.endDate);
}

Actual

https://i.sstatic.net/Mds3M.png

Expected

https://i.sstatic.net/oK5x6.png

Answer №1

After posting, I was able to discover the solution.

this.range.reset();

The form group includes a reset method that clears the calendar selection.

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 JSON response directly in loaded HTML in Angular 2 is a straightforward process. Follow these steps to

As I delve into Angular 2 and explore its components, one question that arises is how to display data received from a server in JSON format on an HTML page. In Angular 1.x, we could directly assign a JSON object to a variable and use it in HTML using the & ...

Alert for every URL modification

Is it feasible to receive (event) notifications for every URL change (without refreshing the page, as possible with location.replaceState())? To clarify: I am not altering components or pages, just updating the URL for future reference. UPDATE Potential ...

How can an Angular Component be created in the browser using the standard method?

Attempting to develop a basic Angular example using JS/ESM. It has been some time since working within the angular environment, and there appear to be two primary choices: Utilizing the UMD lib (preferably to be avoided) Using the ESM2015 folder and loadi ...

Edit the CSS styles within a webview

When loading the page in NativeScript using web viewing, I encountered a need to hide certain elements on the page. What is the best way to apply CSS styles to HTML elements in this scenario? Are there any alternatives that could be considered? I have been ...

Failure to Execute Angular HttpClient Request

I'm facing an issue with firing the HttpClient request. It seems like there might be a problem with importing or providing, but I can't pinpoint where it is exactly. The API works fine, but the call never goes through. Here are the environment/v ...

The CSS overflow scroller trims the excess background color

Attempting to build a website, I encountered an issue with displaying a scroll bar. Despite using the CSS property overflow: auto, I faced another problem. Let me illustrate the issue through a simple example. I have an outer div with the style overflow: ...

Troubleshooting Angular 6: Issues with Route Guards not functioning as expected

Striving to enhance frontend security by restricting access to specific IDs. The goal is to redirect anyone trying to access routes other than /login/:id to a page-not-found error message if not already logged in, but encountering some issues. Below are t ...

Utilize JavaScript libraries in a TypeScript project

Looking to integrate a payment system called iyzico into my project. The iyzico payment library can be found here. However, there are no types available for it. How can I utilize this library in my Node.js TypeScript Express backend project? I attempted t ...

Encountering an issue with Nuxt 3.5.1 during the build process: "ERROR Cannot read properties of undefined (reading 'sys') (x4)"

I am currently working on an application built with Nuxt version 3.5.1. Here is a snippet of the script code: <script lang="ts" setup> import { IProduct } from './types'; const p = defineProps<IProduct>(); < ...

How to access an absolute path defined in a separate workspace

Encountering an issue with cypress. Within my repository, I have 3 workspaces: backend, common, and frontend. package/ backend/ jest.config.cjs tsconfig.json utils/ common/ jest.config.cjs tsconfig.json utils/ helpers/ ...

Ways to remove newly added tasks using JavaScript function?

I have an existing list of li elements in my HTML that can be deleted using JavaScript. However, whenever I add a new li, the delete function no longer works on the newly added item. I suspect the issue lies within the current implementation of the for loo ...

Unidirectional Data Binding in Angular 2 Component Using Plain JavaScript

While working on an Angular2 app using TypeScript, I encountered numerous issues with third-party typings. Frustrated with these problems, I made a bold decision to switch to pure JavaScript. It may sound crazy, especially with the lack of resources availa ...

Example showcasing the functionality of the react-custom-scrollbars package in a TypeScript React application

In my TypeScript React project, I am struggling to set up the react-custom-scrollbars package successfully. Despite consulting the examples provided in the GitHub repository, I have not been able to get it working. Can someone share a functional example ...

Angular API snapshot error: The type 'IJobs' does not match the expected type 'IJobs[]'

Currently, I am in the process of learning and attempting to construct a job board using Angular 10. Although my API setup seems to be functioning properly, when navigating to the job detail page on Chrome, an error is displayed: ERROR in src/app/job-det ...

Enhance a function by sending it back to save static variables

I have a similar function like this one: export const bar = function () { const myItem = new MyItem(); return function bar(param1?: number, param2?: string): void{ ... }; }(); Where myItem is a variable that I use as a temporary inside ...

The error message "printer.node is not a valid Win32 application" indicates that the

I created a node API for my Angular application that utilizes the node-printer package to print PDF files generated by node. However, when I attempted to run my application using nodemon, an error occurred. Error message: "node printer.node is not a val ...

There was an issue with Angular 2.0 at :0:0, which was caused by a response with status: 0 from the URL: null

I am a beginner in Angular 2.0 and I am currently working on creating a sample application using @angular\cli. To serve the application on my local machine, I use the command ng serve --open, which opens it at localhost:4200. Now, I have developed a ...

Unable to download essential dependencies using NPM

After cloning a repository for a MEAN stack application, the first step was to run npm install. The installation process resulted in: added 1580 packages from 1887 contributors and audited 15249 packages in 281.586s found 18 vulnerabilities (5 low, 12 mod ...

The issue of circular dependencies in TypeScript arises specifically within the Record type rather than in an ordinary object type

Can you explain the difference between two types, where one throws a TS error and the other does not? type ScopeItem = | string | { all: string; team: string; }; type ScopesTree = Record<string, ScopeItem | Record& ...

What are the steps for creating a TypeScript version of a custom CKEditor5 build?

Currently, I am in the process of creating a personalized version of CKEditor5. By following the guidelines provided in the official documentation, I successfully obtained ckeditor.js. My goal now is to produce a typescript file (ckeditor.ts or ckeditor.d ...