Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter bootstrap datepicker in Angular js

I am building an application with twitter bootstrap,Angular js and Taffy DB.

javascript:

  $(function() {
   $('#datepicker').datepicker();
    });

   $scope.submit = function () {
   console.log($scope.eventdate);
   };

HTML:

<label for="eventdate">Date</label>
    <div class="input-append date" id="datepicker"  data-date-format="dd-mm-yyyy">
    <input class="span2" size="20" type="text" id="eventdate" ng-model="eventdate">
    <span class="add-on"><i class="icon-th"></i></span>
    </div>

But console.log($scope.eventdate); is undefined.

Please advice

like image 215
user67867 Avatar asked Jan 07 '14 07:01

user67867


2 Answers

If you decided to use Angular, put datepicker into directive:

JS

app.controller('MainCtrl', function($scope) {
  $scope.var1 = '12-07-2013';
});


app.directive('datetimez', function() {
    return {
        restrict: 'A',
        require : 'ngModel',
        link: function(scope, element, attrs, ngModelCtrl) {
          element.datetimepicker({
            dateFormat:'dd-MM-yyyy',
           language: 'en',
           pickTime: false,
           startDate: '01-11-2013',      // set a minimum date
           endDate: '01-11-2030'          // set a maximum date
          }).on('changeDate', function(e) {
            ngModelCtrl.$setViewValue(e.date);
            scope.$apply();
          });
        }
    };
});

HTML

 <div class="container container-fluid" ng-controller="MainCtrl">
       var1={{var1}}
      <form class="form-horizontal" novalidate name="form" ng-submit="submit()">
      <div class="well">
        <div id="date" class="input-append" datetimez ng-model="var1">
          <input data-format="MM-dd-yyyy" type="text" id="input1" name="input1"></input>
          <span class="add-on">
            <i data-time-icon="icon-time" data-date-icon="icon-calendar"></i>
          </span>
        </div>
      </div>

Plunker

like image 186
Maxim Shoustin Avatar answered Oct 18 '22 03:10

Maxim Shoustin


Why not use this: http://angular-ui.github.io/bootstrap/ . It has all the bootstrap modules created as Angular directives, thus no need to re-invent the wheel.

like image 38
CallumVass Avatar answered Oct 18 '22 01:10

CallumVass