How to catch the bootstrap datepicker change event?

后端 未结 4 708
野性不改
野性不改 2021-02-07 04:58

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:

 

        
4条回答
  •  灰色年华
    2021-02-07 05:23

    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:

    
    

    Then tell it which scope function to call:

    
    

    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/

提交回复
热议问题