Within my component, I have incorporated two datepickers. My goal is to emit an object containing the values of both the start date and end date (referred to as startDateValue and

I have attempted to tackle this issue, but I am encountering the problem of receiving the same date value for both inputs.

export class DateComponent implements OnInit {
  @Input() startDate: string;
  @Input() endDate: string;
  @Output() sendDate: EventEmitter<any> = new EventEmitter();
  @Output() emitDate: EventEmitter<any> = new EventEmitter();

  startMinDate: Date;
  startMaxDate: Date;
  endMinDate: Date;
  endMaxDate: Date;


  constructor(private moment: MomentService) {
    this.startMaxDate = this.moment.getMaxDate().toDate();
    this.startMinDate = this.moment.getMinDate().toDate();
    this.endMaxDate = this.moment.getMaxDate().toDate();
    this.endMinDate = this.moment.getMinDate().toDate();
  }

  ngOnInit() {}

  onChange({ value }) {
    const ISOString = value.toISOString();
    this.sendDate.emit(ISOString);
  }

  changedDate({ value }) {
    const ISOString = value.toISOString();
    this.emitDate.emit(ISOString);
  }
}

In the Parent Component.html file, I am looking to connect these two output functions to the child component.

<app-date [endDate]="endDate" (sendDate)="endDate = $event" [startDate]="startDate"
          (emitDate)="startDate=$event"></app-date>

The Child Component.html displays:

<input matInput [matDatepicker]="picker" [value]="endDate" [min]="endMinDate" [max]="endMaxDate"
            (dateChange)="changedDate($event)">
<input matInput [matDatepicker]="picker1" [value]="startDate" [min]="startMinDate" [max]="startMaxDate"
            (dateChange)="onChange($event)">

Answer №1

There is a slight issue with the two methods that trigger when dates change being mixed up. Your code will work correctly if you switch them around. Here is what your code should look like now:

<input matInput [matDatepicker]="picker" [value]="endDate" [min]="endMinDate" [max]="endMaxDate" 
(dateChange)="onChange($event)">  

<input matInput [matDatepicker]="picker1" [value]="startDate" [min]="startMinDate" [max]="startMaxDate"
(dateChange)="changedDate($event)">

Please see the corrected version on this Stackblitz I created based on your code: https://stackblitz.com/edit/angular-eroujf

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

Encountered an issue when attempting to start a fresh project in Angular

Attempting to initiate a new Angular project using the command ng new my-app. The installed versions are as follows: Angular CLI : 6.0 Node version : 10.5.0 Npm : 6.1.0 An error message has been encountered: ERR! path D:\ng\newapp\node_m ...

Encountering an issue when attempting to convert data to JSON in Angular 13, as the content type 'text/plain;charset=UTF-8' is not supported. This problem arises after sending data from

I've been attempting to submit a form using the following method: saveBuildcompany(): void { // @ts-ignore // @ts-ignore console.log(this.group?.value); let data2=this.group.value; let serializedForm = JSON.stringify(data2) ...

Implementing Ionic Native Player for Practical Applications

Looking for a way to incorporate a lightweight mp3 file into my Ionic App, I decided to utilize Native Audio from Ionic. Despite my best efforts, the solution did not function properly on the iOS emulator when called from /MyIonicApp/src/pages/home/home.ts ...

File resolution issue with TypeScript

While I am aware that using TypeScript outFile is generally advised against, I currently have no choice but to utilize it until a more suitable solution like AMD can be implemented. It seems like there may be a "module splitting issue" in my project. In t ...

Automatic Form Saving in Angular 4

Seeking to create a form data autosave feature in Angular 4. The functionality should operate as follows: User modifies data in the form -> save request sent to DB. A timer is initiated for 2 seconds. During the 2-second window after the previous s ...

The use of `super` in Typescript is not returning the expected value

Trying to retrieve the name from the extended class is causing an error: Property 'name' does not exist on type 'Employee'. class Person { #name:string; getName(){ return this.#name; } constructor(name:string){ ...

The module '@types/googlemaps/index.d.ts' cannot be found

I'm working on integrating the Google Maps API into my Angular project and ran into some issues. Here's what I did to install the necessary npm packages: npm install @agm/core --save-dev npm install @types/googlemaps --save-dev Next, I added th ...

Using TypeScript, a parameter is required only if another parameter is passed, and this rule applies multiple

I'm working on a concept of a distributed union type where passing one key makes other keys required. interface BaseArgs { title: string } interface FuncPagerArgs { enablePager: true limit: number count: number } type FuncArgs = (Fu ...

Switching Angular fxLayout from row to column based on a conditional statementHere is an explanation of how to

Visit this link for more information Is there a way to set direction based on a specific value? <div if(value) fxLayout='row' else fxLayout='column'> ...

Is there a way to modify a particular row in a table with Angular 5?

I am facing an issue with a table in my component that contains multiple records. Whenever I try to edit a specific row by clicking the edit button, it ends up making all rows editable instead. Here is how my table looks: The problem lies in the fact tha ...

Showing collections of items in a Kendo Grid column

I am tasked with presenting data in a grid format where the entire array needs to be displayed in one column. To illustrate, let's consider a basic example using the following data structure: export class Person { public Name: string; public ...

"Enhance your database with Firebase's dynamic features for adding and removing

Within my firebase database, I have the following data structure: -ActionSheet -PendingApproval -SomeKey1 -someData -someData -someData -SomeKey2 -someData -someData ...

Angular appears to be unresponsive to callbacks

I currently have a small web application that features a wishlist functionality. While adding items to the list works fine, I am facing an issue with the delete operation in Angular. It seems like Angular does not wait for the response before navigating aw ...

Angular 16 throwing a NullInjectorError within an interceptor

Currently, I am working on setting up a new interceptor within an Angular 16 project. However, when attempting to inject MatSnackBar into the project, I encounter the following error message: NullInjectorError: NullInjectorError: No provider for MatSnack ...

When all the checkboxes have been checked and then one is unchecked, a row is consequently removed

In my Angular table, I have a checkbox column. When I check all checkboxes, they all get checked as expected. Similarly, when I uncheck all checkboxes, they all get unchecked properly. If I check just one checkbox, only that particular one gets checked, ...

Bringing in SCSS using Typescript, React, and Webpack

I am trying to utilize .scss classes by importing them and applying them to the className property of a React component. Here is the structure of my project : root/ ... config/ ... webpack.config.js src/ ... global.d.ts app/ ...

What steps should I follow to bring a telegram bot to life? Is it an issue if I try to integrate a module from Deno

import { TelegramBot, UpdateType } from "https://deno.land/x/telegram_chatbot/mod.ts"; <--- ISSUE import "https://deno.land/x/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c6a2a9b299a3a8b086f6e8f4e8f6"> ...

I am encountering a problem with my component as the Angular Directive is missing

Recently, I incorporated a customized directive into my Angular app to allow file uploads via drag and drop. However, I encountered an issue where the command line kept throwing an error stating that my function does not exist within my component. Propert ...

What causes the focus on input to be lost when using ngFor with a function that returns an array of objects?

When using *ngFor to iterate over an array of objects returned by a function, the focus blocks input preventing any typing. Strangely, this issue does not occur when the array of objects is directly inside the template. What could be causing this problem? ...

Innovative solution for detecting and replacing undefined object properties in Angular 2 with TypeScript

After encountering the issue of core.umd.js:3523 ORIGINAL EXCEPTION: Cannot read property 'fullName' of undefined I realized that the Exception stemmed from a Template trying to access a specific property: {{project.collaborators["0"]["fullN ...