Save state of ng-change local scope in Datepicker

148 Views Asked by At

I have come across what seems like a very simple issue, but I cannot figure out how to fix it. I posted this question originally, but it seems the issue is with the scope.

So basically, this is what is happening:

  • I have a datepicker inside a form. When the user selects a date, ng-change will trigger and it calls the function setDate(date).
  • In debug mode, I confirmed that the selected data indeed is passed to the function.
  • Inside the function, $scope.simStartDate variable is assigned this value and I can see its value changing.
  • However, when I later go to submit the form, $scope.simStartDate is back with its original initialized value.
  • I have put a breakpoint at the initialization $scope.simStartDate = new Date(), but it does not hit. Meaning that the variable is not initialized again.
  • I have tried using a $scope variable in the ng-model, but still the same issue.

This makes me think that ng-change creates a local scope and updates a local variable, which I cannot access later. Is my understanding correct? If not how can I fix this?

This is my HTML:

<div class="row" ng-controller="DashboardParamsFormCtrl">
    <div class="col-sm-6">
        <div class="form-group">
            <label for="inputFirstName">Simulation start date</label>
            <p class="input-group">
                <input type="text" class="form-control"
                       uib-datepicker-popup="{{format}}"
                       datepicker-options="options"
                       ng-model="date"
                       ng-change="setDate(date)"
                       is-open="opened" ng-required="true"
                       close-text="Close"
                       alt-input-formats="altInputFormats"
                       show-button-bar="true" />
                <span class="input-group-btn">
                    <button type="button" class="btn btn-default"
                            ng-click="open()">
                        <i class="glyphicon glyphicon-calendar"></i>
                   </button>
               </span>
            </p>
        </div>
    </div>

This is the JS:

    angular.module('BlurAdmin.pages.dashboard')
        .controller('DashboardParamsFormCtrl', DashboardParamsFormCtrl);

    /** @ngInject */
    function DashboardParamsFormCtrl(baConfig, layoutPaths, baUtil, $scope)
    {
       $scope.ParamsFormBtnClicked = function()
       {
           console.log("Date: " + $scope.simStartDate);
       }

        $scope.open = open;

        $scope.opened = false;
        $scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
        $scope.format = $scope.formats[0];
        $scope.options = {
            showWeeks: false
        };
        function open() {

            $scope.opened = true;
        }

        $scope.simStartDate = new Date();
        $scope.date = new Date();

        $scope.setDate = function(startDate)
        {
            $scope.simStartDate = startDate;
        }

    }
})();

Thank you.

1

There are 1 best solutions below

0
Mahesh Sanjeewa On

your passing new date(); without value, it will get current date. you should pass ng-model value inside new date();

like this.

$scope.simStartDate = new Date(date);
$scope.date = new Date(date); 

hope this link will help you too plnkr