Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I download the date picker code which consist html, css and java script. Its working well. But i want to print or get the selected date from the date picker using angular js. I wrote a code that, but it prints undefined. i dn't know where i was wrong. this is my code.

HTML
<!DOCTYPE html>
<html ng-app>
<head>
  <link rel="stylesheet" type="text/css" href="assets/css/bootstrap.css" />
  <link rel="stylesheet" type="text/css" href="assets/css/todc-bootstrap.css" />
  <link rel="stylesheet" type="text/css" href="assets/css/todc-datepicker.css" />
  
  <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
  <script type="text/javascript" src="assets/js/bootstrap-datepicker.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
  <script src="assets/js/angular.js"></script>
  
  <style type="text/css">
    html,body { margin: 20px; }
  </style>
</head>
<body>
  <form class="form-horizontal" ng-controller="MyController">
    <div class="control-group">
      <label class="control-label" for="dt1">Select Date</label>
      <div class="controls">
        <div class="input-append">
          <input class="span2" id="dt1" type="text" ng-model="selectedDate" />
          <button class="btn" type="button" id="cal1"></button>
        </div> 
		<input class="btn" type="button" value="submit" ng-click="add()"> 
      </div>
	  <h6 class="control-label"> selected date :  </h6>
    </div>
  </form>
</body>


<script type="text/javascript">
$(function() {
    $('#cal1').datepicker({ 
      autoclose: true, 
      todayHighlight: true, 
      targetInput: $("#dt1") 
    });
});
	function MyController($scope) {
        
        $scope.add = function() {
			alert($scope.selectedDate);
        }
    }
</script>

</html>
Posted
Comments
Nathan Minier 17-Nov-14 8:27am    
I'm not having any issues with it, though I don't have the datepicker running.

You are including angular twice, which may be causing your issue.
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script src="assets/js/angular.js"></script>
Satheesh Santhosh 17-Nov-14 8:39am    
The date picker is a textbox. So i'm clear the date and type some text, int or char and click submit, it prints the entered text in alert box. so i think it's not a mistake. Because i tried this after u say, still the problem occur
Nathan Minier 17-Nov-14 9:21am    
I think the issue must be in the datepicker code. You may want to add that to the question.

1 solution

use this code

<!DOCTYPE html>
<html ng-app>
<head>
<link rel="stylesheet" type="text/css" href="assets/css/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="assets/css/todc-bootstrap.css" />
<link rel="stylesheet" type="text/css" href="assets/css/todc-datepicker.css" />

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.3.0/js/bootstrap-datepicker.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>

<style type="text/css">
html, body
{
margin: 20px;
}
</style>
</head>
<body>
<form class="form-horizontal" ng-controller="MyController">
<div class="control-group">
<label class="control-label" for="dt1">Select Date</label>
<div class="controls">
<div class="input-append">
<input class="span2" id="dt1" type="text" ng-model="selectedDate" />
<button class="btn" type="button" id="cal1"></button>
</div>
</div>
<h6 class="control-label">selected date :{{selectedDate}} </h6>
</div>
</form>
</body>


<script type="text/javascript">
$(function () {
$('#cal1').datepicker({
autoclose: true,
todayHighlight: true
}).on('changeDate', function (en) {
var correct_format;
correct_format = en.date.getFullYear() + '-' + ('0' + (en.date.getMonth() + 1)).slice(-2) + '-' + ('0' + en.date.getDate()).slice(-2);
$('#dt1').val(correct_format);

var scope = angular.element($('form[ng-controller="MyController"]')).scope();
scope.safeApply(function () {
scope.selectedDate = correct_format;
});
});
});

function MyController($scope, $rootScope) {
$scope.selectedDate = '';

$rootScope.safeApply = function (fn) {
var phase = this.$root.$$phase;
if (phase == '$apply' || phase == '$digest') {
if (fn) {
fn();
}
} else {
this.$apply(fn);
}
};
}
</script>

</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900