How to catch the bootstrap datepicker change event?
Asked Answered
R

4

19

Now I am trying to do something after user change the date. But seems like my ng-change is ignored.

Here is my code sample:

 <input ng-change='changedate()'
                       type="text" 
                       starting-day="2" 
                       show-button-bar="false" 
                       show-weeks="false" 
                       class="form-control addTicketDateInput" 
                       datepicker-popup="dd MMM" 
                       ng-model="startdate" 
                       is-open="openstart"                         
                       datepicker-options="dateOptions"           
                       ng-required="true"
                       close-text="Close" />

In my controller:

$scope.changedate=function(){
  console.log($scope.startdate);
}

Any idea?

Ransell answered 7/4, 2015 at 17:23 Comment(1)
I think it should work, plnkr.co/edit/QbtoD2ND46ceoDOXes9x?p=preview, click first datepicker and check console outputKirov
R
20

You could keep an eye on when the $scope changed.

Something like this:

$scope.startdate;

$scope.$watch("startdate", function(newValue, oldValue) {
    console.log("I've changed : ", startdate);
});
Reardon answered 3/2, 2016 at 13:45 Comment(0)
V
10

Simple solution: Try passing the value:

HTML

ng-change="selectDate(dt)"

JavaScript

$scope.selectDate = function(dt) {
  console.log(dt);
}
Vanesavanessa answered 8/8, 2017 at 11:25 Comment(0)
L
3

The way I've done this before is to wrap the datepicker jQuery stuff in an angular directive. This is rough but here's the idea:

app.directive('dateDirective', function() {
    return {
        restrict: 'A',
        scope: {
            onChange: '&'
        },
        link: function(scope, element, attrs) {
          element.datetimepicker({
            format: "MM-yyyy"
            //all your options here
          }).on('changeDate', function(e) {
            scope.$apply(function(scope) {
              scope.onChange(e.date);
            });
          });
        }
    };
});

You can capture the change event from the jQuery element and use it to call a function in your controller, or just use it to set scope values or whatever:

var app = angular.module('myApp', []);

app.controller('myAppCtrl', function($scope) {
  $scope.current = '';
  $scope.changedate = function(date){
    $scope.current = date;
  };
});

Then you just need to add the directive to your dom element:

<input date-directive
                   type="text" 
                   starting-day="2" 
                   show-button-bar="false" 
                   show-weeks="false" 
                   class="form-control addTicketDateInput" 
                   datepicker-popup="dd MMM" 
                   ng-model="startdate" 
                   is-open="openstart"                         
                   datepicker-options="dateOptions"           
                   ng-required="true"
                   close-text="Close" />

Then tell it which scope function to call:

<input date-directive onChange='changedate(date)'
                   type="text" 
                   starting-day="2" 
                   show-button-bar="false" 
                   show-weeks="false" 
                   class="form-control addTicketDateInput" 
                   datepicker-popup="dd MMM" 
                   ng-model="startdate" 
                   is-open="openstart"                         
                   datepicker-options="dateOptions"           
                   ng-required="true"
                   close-text="Close" />

Like I said this is rough just recalled it quickly. If this doesn't help I'll dig out a sample that is tested.

Hope that helps!

also, there's a datepicker directive here that might be halpful:

https://angular-ui.github.io/bootstrap/

Langille answered 7/4, 2015 at 19:12 Comment(0)
S
1

Simple Solution In Angular

(bsValueChange)="Function($event)"
Snowshed answered 7/6, 2020 at 21:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.