Issue arising in Angular 7 due to the binding between the TypeScript (ts) file and HTML file for the property [min]

I am currently utilizing Angular Cli version 7.3.9

In my project, I have implemented a date type input that is intended to display, in its datepicker, starting from the next day after the current date.

This is how I approached it in my .ts file:

debugger
var minDateFinal ;
this.minDate = new Date();
this.minDate.setDate(this.minDate.getDate()+1)
minDateFinal = this.datePipe.transform(this.minDate,'yyyy-MM-dd') ;
console.log(minDateFinal);
debugger

And here is what I included in my .Html file:

<input type="date" class="form-control" formControlName="dateDebut" [min]="minDateFinal">

After checking the browser console, I can see that the correct result is being logged, but unfortunately, the expected behavior is not reflected on the UI. Below are screenshots illustrating the issue:

When I manually set the 'min' attribute with a hardcoded date like so:

<input type="date" class="form-control" formControlName="dateDebut" min="2020-10-02">

I achieve: Desired Outcome

However, when implementing my dynamic approach, the desired functionality does not occur.

I observe: Actual Result

Thank you for any assistance provided.

Answer №1

The reason behind this is that when you initialize your date in the ts file, the view (html) is not yet ready. To solve this issue, you need to utilize ngAfterViewInit() method in your ts file:

minDateFinal;
minDate;

ngAfterViewInit() {
  this.minDate = new Date();
  this.minDate.setDate(this.minDate.getDate()+1)
  this.minDateFinal = this.datePipe.transform(this.minDate,'yyyy-MM-dd') ;
}

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

Stopping the subscription to an observable within the component while adjusting parameters dynamically

FILTER SERVICE - implementation for basic data filtering using observables import { Injectable } from '@angular/core'; import { BehaviorSubject, Observable } from 'rxjs'; import { Filter } from '../../models/filter.model'; imp ...

Using two variables for iteration in Vue.js v-for loop

Can you create a v-for loop with two variables? I attempted the following, but it did not function as expected <ul id="example-1"> <li v-for="apple in apples" v-for="banana in bananas"> {{ apple .message }} {{ banana .message }} & ...

next-intl failing to identify the primary language setting

When testing next-intl for the app directory in the Next.js v13.4.0, I encountered an issue where the default locale was not recognized. Despite following the documentation step by step, I also faced significant challenges with the client-side version in p ...

Executing an SQL delete query with a button click using a JavaScript function in PHP

I have created a setup with three essential files - index.html, database.php, and function.js. In database.php, there is a form generated containing a delete button that triggers the deletion SQL query when clicked. The primary objective is to present a ta ...

Verify the content of each file in a bulk upload before transferring them to the server

I am facing an issue with a form that has 3 separate file input fields. I need to validate their MIME types individually before uploading them to the server. The first two should only allow MP3 files, while the last one should only allow JPEG files. Is th ...

When using threejs, the color set for setClearColor is supposed to be white. However, when calling an external HTML file, it unexpectedly renders as

When I call an external HTML file in Three.js, the value for setClearColor is white but it renders as black. How can I solve this issue? Click here to view the image Here are the codes from the external file: <div id="3d-modal"></div> <sc ...

What is the best way to present CouchDB design documents in a clear and reader-friendly format?

Struggling to define views in a readable way because they are set in JSON in CouchDB. Consider this example: { "language": "javascript", "views": { "by_location": { "map": "function(doc) { if (doc.location != null) emit(doc.l ...

Exploring the Unpredictable Results of Recursive Functions in JavaScript

Take a look at this recursive code snippet. function calculateFactorial(n) { if (n === 0 || n === 1) { return 1; } else { console.log(calculateFactorial( (n - 1) )); return n * calculateFactorial(n - 1); } } const num = ...

Struggling to integrate data retrieved from a Vue.js API

Once I receive the API call with an attached string input, I successfully get the result. However, I am struggling to incorporate it into my frontend. I have attempted various solutions found online without success and cannot seem to grasp the concept. Her ...

Most effective method for adding JQuery events to dynamically generated buttons

I am dynamically generating Twitter Bootstrap modals based on user actions (Long Story). Sometimes, the user may see up to 100 modals on their screen. Each modal contains 5 dynamic buttons, each serving a different purpose with unique ids. I am using jQue ...

Windows authentication login only appears in Chrome after opening the developer tools

My current issue involves a React app that needs to authenticate against a windows auth server. To achieve this, I'm hitting an endpoint to fetch user details with the credentials included in the header. As per my understanding, this should trigger th ...

Tips for maintaining the state in a React class component for the UI while navigating or refreshing the page

Is there a way to persist the selection stored in state even after page navigation? I have heard that using local storage is a possible solution, which is my preferred method. However, I have only found resources for implementing this in functional compone ...

What is the best way to assign a value to a property in a Controller or Global Variable using jQuery?

On my ASP MVC 5 site, I have a checkbox within the NavBar element. This checkbox's state dictates whether or not the Grid will display Inactive records alongside active ones on each page. In an attempt to have all controllers access the same property ...

Content moves as you scroll, not within a lightbox

I have implemented lightbox style overlays for my 'Read More' content on my website. However, I am facing an issue where the content within the lightbox does not scroll; instead, the background page scrolls. Any assistance with resolving this p ...

Are generic constraints leading to type inference selecting the incorrect candidate?

TypeScript Version: 2.6.0-dev.20170826 and 2.4.2 I'm questioning whether I've encountered a TypeScript inference bug or limitation, or if my code is simply incorrect. If the code is valid and it's an issue with type inference, I will repor ...

Chrome timing out when sending an express post request

I am a beginner in API development and encountering an issue with a post request timing out in Chrome. The error message I keep receiving is net::ERR_EMPTY_RESPONSE. I have included my JavaScript code below where I am attempting to send data to the API. Th ...

What is the process for exporting Three.js files to .stl format for use in 3D printing?

I stumbled upon a page with this link: Is it possible to convert Three.js to .stl for 3D printing? var exporter = new THREE.STLExporter(); var str = exporter.parse(scene); console.log(str); However, despite using the code provided, I am unable to export ...

In the event that the p-value is equal to a non-functioning

Just started delving into the world of JavaScript and jQuery. Take a look at the code snippet below: $(document).ready(function(){ $("#p1").click(function(){ var input = document.getElementById("p3"); if (input.value == "Hi ...

Can you explain the correct method for assigning types when destructuring the `callbackFn.currentValue` in conjunction with the `.reduce()` method? Thank you

I'm working with an array of arrays, for example: const input = [['A', 'X'], ['B', 'Y'],...]; In addition to that, I have two enums: enum MyMove { Rock = 'X', Paper = 'Y', Scis ...

Instructions for activating column resizing in MUI DataGrid

Is there a way to enable column resizing for users in MUI DataGrid? It's enabled by default on XGrid, but I would like to enable it on Datagrid as well. Any assistance is appreciated. <DataGrid className={classes.table} ...