简体   繁体   English

angular-daterangepicker:如何读取更新的模型

[英]angular-daterangepicker: how to read updated model

I'm using the module "angular-daterangepicker" (from here ) that has to be bound to a scope-model. 我正在使用必须绑定到范围模型的模块“ angular-daterangepicker”(从此处开始 )。 When I do this, it initializes correctly. 当我这样做时,它将正确初始化。 But on updates, this model does not change at all: 但是在更新时,此模型完全不变:

HTML: HTML:

<input date-range-picker type="text" ng-model="dateRange" 
 options="dateRangeOpts" />

Javascript: 使用Javascript:

$scope.dateRange = {
  startDate: '2015-03-01',
  endDate: '2015-03-05'
};
$scope.dateRangeOpts = {
  format: 'YYYY-MM-DD'
};

// watch: Not working
$scope.$watch('dateRange', function(newDate) {
  console.log('New date set: ', JSON.stringify(newDate));
}, false);

When changing the dateRange directly, $watch is called. 直接更改dateRange ,将调用$watch When using the daterangepicker, nothing happens. 使用daterangepicker时,什么也不会发生。

Since the watch mechanism is taken from the example and the angular-wrapper seems to be active, I assume I'm doing something wrong. 由于监视机制来自示例,并且角度包裹器似乎处于活动状态,因此我认为我做错了什么。 How do I get the updated model? 如何获得更新的模型?

As a workaround, I'm reading the input-value using JQuery and ng-change . 解决方法是,我使用JQuery和ng-change读取输入值。

You need to use following code on change function of datapicker 您需要在数据选择器的更改功能上使用以下代码

$scope.$apply(
     function(){
        $scope.varible = date;
    }
);

I encountered the same issue. 我遇到了同样的问题。 It works in my hands only if I use a dict for the dates. 仅当我对日期使用字典时,它才有效。 This works: 这有效:

    $scope.daterangepicker = {
        dates : { startDate: null, endDate: null } 
        };
    $scope.$watch("daterangepicker.dates", function(newDates, oldDates) {
        console.log("New date set: ", newDates);
    }, false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM