0

So I've already searched quite a bit on stackoverflow, and it seems the issues are similar to mine, but different. For me, the view (div class with id="reportrange") is updating right away to show the date selected when the user selects their desired date range, but the $scope variable (used so that I can pass the selected start and end date values into a filter) is not updating until the user clicks on the reportrange div a second time.

I'm using the bootstrap date range picker with AngularJS.

Here is the code In my controller:

//start date when page loads
$scope.startDate = moment().subtract(3, 'years').startOf('year').format('YYYY-MM-DD');    
//end date when page loads
$scope.endDate = moment().add(2, 'years').endOf('year').format('YYYY-MM-DD');  

//updates the startDate and endDate
var updateStartEnd = function (start, end) {
    $scope.startDate = start.format('YYYY-MM-DD');
    $scope.endDate = end.format('YYYY-MM-DD');
    //$log.info($scope.startDate);
    //outputs the correct date but date not changed on the page
    //$log.info($scope.endDate);
    //outputs the correct date but date not changed on the page
}

$scope.dateFunc = function () {

    function cb(start, end) {
        updateStartEnd(start, end);
        $('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY'));
    }

    $('#reportrange').daterangepicker({

        ranges: {
           'Last 7 Days': [moment().subtract(6, 'days'), moment()],
           'Last 30 Days': [moment().subtract(29, 'days'), moment()],
           'This Month': [moment().startOf('month'), moment().endOf('month')],
           'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')],
           'This Year': [moment().startOf('year'), moment().endOf('year')]
        }

    }, cb);
};

and this is date range picker dropdown on my html page:

<div ng-click="dateFunc()" id="reportrange" class="pull-right" style="background: #fff; cursor: pointer; padding: 5px 10px; border: 1px solid #ccc; width: 100%">
<i class="glyphicon glyphicon-calendar fa fa-calendar"></i>&nbsp;
<span>Select a Date Range...</span> <b class="caret"></b>
</div>

I've tried adding a $scope.watch as well as changing the ng-click to ng-init, but neither of those proved to be useful. Any help is greatly appreciated.

2
  • You are updating a model outside angular's digest cycle. you should use $scope.$apply() when the model is updated, IE when the daterangepicker's model is updated (onChanged callback or something similar). Commented Jun 9, 2016 at 23:08
  • Thank you! (Solution posted below) Commented Jun 13, 2016 at 17:38

1 Answer 1

3

I fixed the issue by adding $scope.$apply as seen below:

//updates the startDate and endDate
var updateStartEnd = function (start, end) {
    $scope.$apply(function () {
        $scope.startDate = start.format('YYYY-MM-DD');
        $scope.endDate = end.format('YYYY-MM-DD');       
    });
};
Sign up to request clarification or add additional context in comments.

Comments

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.