Exploring the functionality of ng2-bootstrap to implement a datepicker in popup mode

In my current project, I am utilizing the ng2-bootstrap date-picker. The goal is to display the date-picker when clicking on the calendar icon and hide it once a date has been selected.

<label>Date:</label> 
<div class="datepickerDiv">
   <input type="text" value="{{ getDate() | date:'dd-MMMM-yyyy' }}" class="datepicker"> 
        <span class="cal-icon" (click)="open()"><i class="fa fa-calendar-check-o" aria-hidden="true"></i></span>
        <span class="clearDate" (click)="clearDate()"><i class="fa fa-times-circle" aria-hidden="true"></i></span>
        <ul class="datepicker-ul" role="menu" *ngIf="opened">
             <datepicker [(ngModel)]="Date" [minDate]="minDate" [showWeeks]="false"></datepicker>
        </ul>
</div>

The component setup that I am using is as follows:

private opened: boolean = false;
public open():void {
    this.opened = !this.opened;
}

public getDate():number {
    return this.Date && this.Date.getTime() || new Date().getTime();
}

private clearDate() {
    this.Date = null;
}

However, I have encountered an issue where the date-picker only closes upon clicking the icon again.

Answer №1

To improve your code, create separate functions for "open" and "close" actions rather than using a toggle function. You can then utilize the event selectionDone($event) (which is not yet documented) to close the datepicker.

<datepicker [(ngModel)]="dt" [minDate]="minDate" [showWeeks]="false" (selectionDone)="onSelectionDone($event)"></datepicker>

In your code, implement the following:

public onSelectionDone(a) {
    this.close();
}

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

Is your attention on directing an inline jQuery UI Datepicker towards utilizing keyboard shortcuts effectively?

After scouring the web for a solution or example without success so far, I am turning to this community for help. I have found that jQuery UI offers convenient keyboard shortcuts for the Datepicker widget, but they only seem to function if the widget is a ...

akka-http integrated with angular2 routing

Currently, I am facing an issue with serving my Angular2(rc3) app from akka-http (a Scala rest framework). The problem lies in routing. Whenever a request is made, spray attempts to locate /my/endpoint/declared/in/angular as a regular resource. How can I r ...

Encountering a TypeError while working with Next.js 14 and MongoDB: The error "res.status is not a function"

Currently working on a Next.js project that involves MongoDB integration. I am using the app router to test API calls with the code below, and surprisingly, I am receiving a response from the database. import { NextApiRequest, NextApiResponse, NextApiHandl ...

Retrieve the data from the mat-checkbox

My goal is to retrieve a value from a mat-checkbox, but the issue is that we only get boolean expression instead of the string value. Here's an example snippet of what I'm looking for: <mat-checkbox formControlName="cb2" <strong&g ...

When implementing Typescript, utilizing the useContext() function may result in the following error: "Cannot assign type 'never[]' as an argument to the parameter type

When attempting to utilize the useContext() hook in TypeScript, I encountered several errors. Initially, useState is set to an empty array of the interface Phone which is imported. The state and setState are then passed in the value prop. import { createCo ...

Troubleshooting the unexpected behavior of the shareReply() method in Angular version 15

Utilizing the same API across 3 components can lead to duplicate HTTP calls. To prevent this, I decided to cache the response using shareReply() from RxJs so it can be reused wherever needed. Here's how I implemented it: api-service.ts getUsers(): Ob ...

Implementing Default Language in Next.js 14 for Static Export without URL Prefix: A Step-by-Step Guide

Currently, I am in the process of developing a website using Next.js 14, with the intention of exporting it as a static site for distribution through a CDN (Cloudflare Pages). The website I am working on requires support for internationalization (i18n) to ...

Incorporating an additional row into a material table

Currently, I have implemented a mat-table with various features including drag and drop functionality and dynamic columns. However, I am facing an issue while trying to add row filters under the table header. I attempted to simply insert a <mat-row> ...

Accessing the return value from an Angular subscription and storing it in

How can I use the value from a subscription to set the property for returning date and time? Component ngOnInit() { this.resetForm(); let defaultWIPEndTime = this.service.getDefaultWIPEndTime().subscribe(res => {}); console.log(defaultW ...

Angular 2 Loading Indicator for HTTP Requests

Is there a way in Angular 2 to track the progress (percentage completed) of an ajax call using the angular2/http module? Below is the code snippet I currently use for my HTTP requests: let body = JSON.stringify(params); let headers = new ...

Invoke a method on VUE2 DateSelect

My issue involves using the VUEJS2 Datepicker and attempting to call a function from the methods. However, I am encountering difficulties as it is not working. There are no errors displayed in the console. Interestingly, if I try to call the function on an ...

Changes in tabs are discarded when switching between them within Material UI Tabs

I have been experiencing an issue with the Material UI tab component where changes made in tabs are discarded when switching between them. It seems that after switching, the tabs are rendered again from scratch. For example, let's say I have a textFie ...

The Angular Universal Starter is running smoothly on local environments, but encountering difficulties when attempting to launch on the

My current challenge involves running Angular Universal Starter on a Centos server. Executing build:ssr and serve:ssr locally works without any issues. After transferring the dist folder to the server, I updated nodejs, npm, and pm2 to their latest ver ...

Simulating Express Requests using ts-mockito in Typescript

Is there a way to simulate the Request class from Express using ts-mockito in typescript? I attempted the following import { Request, Response } from "express"; const request = mock(Request); const req: Request = instance(request); but encou ...

Toggle the visibility of a component by clicking on a specific title within a table, dependent on the column title in Angular 9

Currently, I am developing an Angular application focused on creating a COVID-19 tracking app. In this project, I have designed 2 components - Component A displays a list of all states, while Component B lists all districts within a particular state. To ...

Tips on providing validation for either " _ " or " . " (select one) in an Angular application

I need to verify the username based on the following criteria: Only accept alphanumeric characters Allow either "_" or "." (but not both) This is the code snippet I am currently using: <input type="text" class="form-control" [ ...

Obtain unique entries from a Firestore collection search

Seeking assistance in filtering an observable to only contain unique records. I am using the observable in an angular mat-autocomplete with an async pipe and querying firebase based on the user's input. The code for the mat-autocomplete template: ...

What is the best approach to managing a 204 status in Typescript in conjunction with the Fetch API

Struggling to handle a 204 status response in my post request using fetch and typescript. I've attempted to return a promise with a null value, but it's not working as expected. postRequest = async <T>(url: string, body: any): Promise ...

Absolute imports in create-react-app do not function properly when using yarn v2 workspaces alongside typescript

I am currently utilizing yarn v2 workspaces, and within my workspaces, I have a frontend project built using create-react-app / react-scripts. My goal is to enable absolute imports in the frontend application so that I can simply do things like import Butt ...

Inferring object types through direct values

This code example has a lot of detail: interface Coordinate { latitude: 40.7128; longitude: -74.0060; } const location: Coordinate = { latitude: 40.7128, longitude: -74.0060, }; // The inferred type would have been // { x: number; y: number; } I ...