1

I want to use this plugin in AngularJS in my export table with filters. is it possible to use it with ng-model and all other uses

the plugin : https://uxsolutions.github.io/bootstrap-datepicker/?markup=input&format=&weekStart=&startDate=&endDate=&startView=0&minViewMode=0&maxViewMode=4&todayBtn=false&clearBtn=false&language=en&orientation=auto&multidate=&multidateSeparator=&keyboardNavigation=on&forceParse=on#sandbox

especially I need the "range" -from/to . option

5
  • 1
    Is is angular or angular js ? Commented Aug 20, 2017 at 7:51
  • use ui-bootstrap datepicker, if you are using angularJS Commented Aug 20, 2017 at 8:05
  • If you want to use bootstrap in combination with angularjs, I advise you to use UI-Bootstrap. These are AngularJS directives for bootstrap. It also has the bootstrap datepicker. angular-ui.github.io/bootstrap/#!#datepicker Commented Aug 20, 2017 at 9:45
  • Possible duplicate of Best way to combine AngularJS and Twitter Bootstrap. Commented Aug 20, 2017 at 11:13
  • UI-bootstarp date picker is not good enough case there is no option to do "range" option like in jquery Commented Aug 20, 2017 at 14:26

1 Answer 1

2

To use uxsolutions bootstrap-datepicker in AngularJS, create a custom directive like this:

app.directive("datepicker", function() {
    return {
      restrict: "A",
      require: "ngModel",
      link: function(scope, elem, attrs, ngModelCtrl) {
          elem.on("changeDate", updateModel);
          elem.datepicker({});

          function updateModel(event) {
              ngModelCtrl.$setViewValue(event.date);
          }
      }
    }
})

Usage:

<div datepicker id="myDatePicker" ng-model="myDate"></div>
<div> {{myDate | date }} </div> 

The DEMO

angular.module("app",[])
.directive("datepicker", function() {
    return {
      restrict: "A",
          require: "ngModel",
          link: function(scope, elem, attrs, ngModelCtrl) {
            elem.on("changeDate", updateModel);
            function updateModel(event) {
              ngModelCtrl.$setViewValue(event.date);
            }
            elem.datepicker({});
          }
    };
})
<script src="//unpkg.com/jquery"></script>
    <script src="//unpkg.com/bootstrap-datepicker"></script>
    <script src="//unpkg.com/angular/angular.js"></script>
    <link href="//unpkg.com/bootstrap-datepicker/dist/css/bootstrap-datepicker.standalone.css" rel="stylesheet">
  <body ng-app="app">
    <div>Selected date: {{myDate | date}}</div>
    <div datepicker ng-model="myDate"></div>
  </body>

Sign up to request clarification or add additional context in comments.

2 Comments

Can I use the ng-model also in the controller ? why do I need ngModel
ng-model is used for two way data binding. To get the ng-model value in controller we use $scope . For example if you want to get value myDate from above example you will use $scope.myDate in controller.

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.