2

I'm new to angular.

I have an input date picker. I want to get/fetch the date selected in the date picker.

Code: Html file:

 <input class="form-control m-input " id="m_datepicker_2_validate" placeholder="Select Date" readonly="" type="text">

I'm trying to console.log the selected date.
In the component.ts file I've added a variable staff_date_booking : Date; and created a function

bookStaffEmployeeDate(){
        console.log("This is the DATE:", this.staff_book_date);
    }

and in the html I've added

 <input [ngModel]="staff_book_date | date:'yyyy-MM-dd'" class="form-control m-input " id="m_datepicker_2_validate" placeholder="Select Date" readonly="" type="text">

Once the date is selected I'm calling this function on (click) but I'm getting this.staff_book_date as undefined.

How can I store the selected date in a variable like staff_book_date

5
  • try changing [ngModel] to [(ngModel)] in your input tag. Commented Oct 25, 2018 at 6:08
  • Did that, still getting undefined. Commented Oct 25, 2018 at 6:14
  • maybe you are calling bookStaffEmployeeDate() before the value of staff_book_date is selected Commented Oct 25, 2018 at 6:28
  • No, it is after the date is selected. There's another input after the date picker and on its click I've called the function. Commented Oct 25, 2018 at 6:31
  • try stackblitz.com/edit/angular-vbukxm?embed=1&file=src/app/… Commented Oct 25, 2018 at 7:56

2 Answers 2

1

Don't use pipes inside ngModel. Also, use the two-way binding in the ngModel

<input [(ngModel)]="staff_book_date" 
Sign up to request clarification or add additional context in comments.

4 Comments

Removed pipes and added two way binding in ngModel, still it says undefined.
No console errors after removing pipes. Console output: This is the DATE: undefined
where do you call the bookStaffEmployeeDate methos
There is another input field right after the date picker I've added (click)="bookStaffEmployeeDate()" on the input field.
1

Create a property in your .ts file and make a two way binding with it in your .html

Make the following changes

.component.ts

staff_book_date : any;

bookStaffEmployeeDate(){
        console.log("This is the DATE:", this.staff_book_date);
}

component.html

<input [(ngModel)]="staff_book_date" class="form-control m-input " id="m_datepicker_2_validate" placeholder="Select Date" type="date">
<button (click)="bookStaffEmployeeDate()">Click Me</button>

9 Comments

If I try to create a 2 way binding model it gives an error regarding the pipes. Error I get in console log: ERROR Error: Uncaught (in promise): Error: Template parse errors: Parser Error: Cannot have a pipe in an action expression at column 19 in [staff_book_date | date:'yyyy-MM-dd'=$event] in ng:///ServiceModule/ServicesComponent.html@809:40 ("<div class="input-group date">
Remove the pipe in two way binding.
if you want to format your input using pipe, check this link
After removal of pipe I get no error but the console log shows date as undefined. Console log: This is the DATE: undefined
if you are not entering any value in input box, it will be undefined since we have declared staff_book_date not defined it. Also, you have made your input as readonly, you wont be able to edit it also.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.