Angular-ui bootstrap datepicker won't openup twice - angularjs

I am using the angular-ui bootstrap datepicker component. on one view where I have two date fields, the datepicker opens up and after closed it opens up again when clicked fine.
On another page, after once opened and closed, the datepicker does not open up anymore.
Surely I'm missing something here, but I gave up after hours of trying to find it.
Can you help ?
the HTML section of the datepicker looks as follows:
<div class="input-group">
<input type="text" class="input-sm-7" datepicker-popup="dd/MM/yyyy" is-open="opened"
datepicker-options="dateOptions" ng-model="singleDate"
close-text="Close" />
<button type="button" class="btn btn-default" ng-click="open($event)"><i
class="fa fa-calendar"></i>
</button>
</div>
The JS code in the controller looks like this:
$scope.dateOptions = {
formatYear: 'yy',
startingDay: 1
};
$scope.open = function($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.opened = true;
};

This is a known issue and there is a hack for it. Try this.
$scope.datePicker = {};
$scope.open = function ($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.datePicker.opened = true;
};
Change in the Html
<div class="input-group">
<input type="text" class="input-sm-7" datepicker-popup="dd/MM/yyyy" is-open="datePicker.opened"
datepicker-options="dateOptions" ng-model="singleDate"
close-text="Close" />
<button type="button" class="btn btn-default" ng-click="open($event)"><i
class="fa fa-calendar"></i>
</button>
</div>

Related

Angular datepaicker works in minial project, not in full-blown

I have a bare bones, minimalistic app, which includes this in the HTML of a view
<input type="text" class="form-control" uib-datepicker-popup="{{format}}" ng-model="dt"
is-open="popup1.opened" min-date="minDate" max-date="maxDate"
datepicker-options="dateOptions" date-disabled="disabled(date, mode)"
ng-required="true" close-text="Close"
alt-input-formats="altInputFormats" />
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="open1()">
<i class="glyphicon glyphicon-calendar"></i></button>
</span>
and this in the controller
$scope.open1 = function() {
$scope.popup1.opened=true;
}
$scope.popup1 ={'opened': false}
$scope.dt = new Date();
When I click the calendar icon in the demo project, the calendar pops open, but it does not in my full-blown project, even when I copy/paste the code ($scope.popup1.opened does change from false to true when clicked).
The full blown project is too large to post - any advice?
[Update] Thanks for the help. One point to notice is that the code works in a minmial project, but not in a full-blown project, regardless of the suggested duplicate question.
However, I have taken on board the suggested duplicate an, alas, it does not help.
$scope.SwallowClick = function($event)
{
if ($event.cancelable)
$event.preventDefault();
$event.stopPropagation();
return false; // don't handle event further
}
$scope.open1 = function($event)
{
$scope.SwallowClick($event);
$scope.popup1.opened=true;
return false; // don't handle event further
}
Have you seen this question and the accepted answer? ui.bootstrap.datepicker is-open not working with a button
Try passing $event to your ng-click handler, like this...
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="open1($event)">
<i class="glyphicon glyphicon-calendar"></i></button>
</span>
And then in your javascript, something like this...
$scope.open1 = function($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.popup1.opened=true;
}
$scope.popup1 ={'opened': false}
$scope.dt = new Date();
D'oh! $scope.formatwas not defined (blush)

angularjs - datepicker popup opens both in start date and end date

How can I prevent the datepicker popup from opening at the same time when I click on only 1 input?
Plunkr
<form>
<div class="form-group">
<label for="fromDate">From:</label>
<input type="text" class="form-control" datepicker-popup="{{format}}" ng-model="dt" is-open="opened" datepicker-options="dateOptions" ng-required="true" close-text="Close" show-weeks="false" ng-click="open($event)" name="fromDate" id="fromDate" />
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="open($event)">
<i class="glyphicon glyphicon-calendar"></i>
</button>
</span>
</div>
<div class="form-group">
<label for="toDate">To:</label>
<input type="text" class="form-control" datepicker-popup="{{format}}" ng-model="toDate" is-open="opened2" datepicker-options="dateOptions" ng-required="true" close-text="Close" show-weeks="false" ng-click="open($event)" name="toDate" id="toDate" />
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="open($event)">
<i class="glyphicon glyphicon-calendar"></i>
</button>
</span>
</div>
</form>
I looked at your plunkr. In your script.js file you have the following
$scope.open = function($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.opened = true;
$scope.opened2 = true;
};
$scope.opened = true; and $scope.opened2=true; are both being set. This is forcing both of them to open. You are calling this function when either of the calendar buttons is clicked
ng-click="open($event)"
is-open will force the calendar to show when the variable is set somewhere else in code as well. Remember it is 2-way binding
As for your solution. A simple way I can think to fix it is to pass another variable into your open method telling it which button was clicked so
$scope.open = function($event, calId) {
$event.preventDefault();
$event.stopPropagation();
if(calId===1) $scope.opened = true;
if(calId===2) $scope.opened2 = true;
};
Then in your html for the buttons change to the following passing in a 1,2,3 etc.. for whichever datepicker on the page you want to display.
ng-click="open($event,1)"

Can't open Datepicker using calendar button

Here's my code:
<p class="input-group">
<input type="text" class="form-control date" id="startDate" placeholder="Start Date" ng-click="date.startDate=true" datepicker-popup="MM/dd/yyyy" ng-model="date.start" is-open="date.startDate" date-disabled="disabled(date, mode)" ng-required="true" close-text="Close">
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="date.startDate=true" is-open="date.startDate"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</p>
For some reason, I can't get the calendar button to open the datepicker up... I thought hvaing just ng-click would work but that isn't working.
Here is the controller code:
self = this;
self.open = function ($event) {
$event.preventDefault();
$event.stopPropagation();
self.opened = {};
self.opened[$event.target.id] = true;
// log this to check if its setting the log
console.log(self.opened);
};

Angularjs ui-bootstrap datepicker is not working in my application

Here is code which I am to trying to run in my application. Even I have the same versions of js and css. here is the plunker link. demo of original code, here.
//html code
<div ng-controller="DatepickerDemoCtrl">
<h4>Popup</h4>
<div class="row">
<div class="col-md-6">
<p class="input-group">
<input type="text" class="form-control" datepicker-popup="yyyy-MM-dd" ng-model="dt" is-open="opened" />
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="open($event)"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</p>
</div>
</div>
</div>
//Js code
var DatepickerDemoCtrl = function ($scope) {
$scope.open = function($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.opened = true;
};
};
This code I have applied exactly same in my controller, I have included dependency modules also, I have same versions as in plunker. but is not working in my application. I am struggling from last 2 days. Please somebody let me know the reason.
Here is my html
<h4>Popup</h4>
<div class="row">
<div class="col-md-6">
<p class="input-group">
<input type="text" class="form-control" datepicker-popup="yyyy-MM-dd" ng-model="dt" is-open="opened" />hi{{dt}}
<span class="input-group-btn">
<button type="button" class="btn btn-default" ng-click="opencal($event)"><i class="glyphicon glyphicon-calendar"></i></button>
</span>
</p>
</div>
</div>
....//my ctrl is home ctrl here so this code wrapped in homectl.
Here is my js code
//app code.
var app1 = angular.module("HomeApp",['uiSlider','ui.bootstrap']);
//this where i defined dependency.
app1.controller('HomeCtrl', function( $scope,$http,$q,$location,$window,$routeParams,$modal, $log) {
//here is my datepicker code.
$scope.opencal = function($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.opened = true;
};
...//my rest code goes below
Queries I have is
I cant see date when I select date from calendar.(in {{dt}}).
I need to pass selected date to my home controller.

bootstrap angular datepicker set ng-model $invalid not change

I am Useing Bootstrap angular datepicker in a form to update my model 'User'.
I set this datepicker 'require' property is true.
UpdateUser.html:
<form name="userForm" novalidate>
<div class="form-group">
<label>Birthday</label>
<p class="input-group">
<input type="text" name="birthday" class="form-control" datepicker-popup="yyyy-MM-dd" ng-model="user.birthday" is-open="opened" datepicker-options="dateOptions" ng-required="true" />
<span class="input-group-btn">
<button type="button" class="btn btn-default"
ng-click="openDate($event)">
<i class="glyphicon glyphicon-calendar"></i>
</button>
</span>
</p>
<span>{{userForm.birthday.$invalid}}</span>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary" ng-disabled="userForm.$invalid">Update</button>
</div>
</form>
UserCtrl.js:
mainApp.controller("detailCtrl", function($scope, $http, $routeParams, DemoService) {
$scope.user = DemoService.get({userId : $routeParams.userId});
$scope.openDate = function($event) {
$event.preventDefault();
$event.stopPropagation();
$scope.opened = true;
};
$scope.dateOptions = {
formatYear: 'yy',
startingDay: 1
};
});
So my question is:
Why {{userForm.birthday.$invalid}} is true when I first set the 'user.birthday' to the datepicker?
Plunker:
http://plnkr.co/edit/DkGEVJ?p=preview
This is weird, but changing the initial birthday from '1403575208000' to new Date(1403575208000) fixes this problem.
http://plnkr.co/edit/Wut4sJT5hFcG6EfSYe8t?p=preview
There are a lot of different problems that look the same at angular-bootstrap Github forums.
I am using angular-bootstrap version 0.13.0 and I found that the datepicker has the value:
userForm.$invalid = true
when the date field is empty and other situations, and does not work well.
When you want to validate it, problems arise. In such cases, to avoid datepicker validation I used the following code:
<div class="form-group">
<button type="submit" class="btn btn-primary" ng-disabled="userForm.$error['required'] !== undefined">Update</button>
</div>
Here is my plunkr
http://plnkr.co/edit/Sqog7OqudQkww9WsCUT2?p=preview
There you can see the value of userForm.$error and decide to use ng-required=true or false at 'birthday'.
Hope it helps!

Resources