1

In AngularJS, I have on the DOM side:

<input class="form-control"  type="date" ng-model="scope.status.date"
       ng-change="scope.reloadDay()" ng-model-options="{debounce:200}">

In the back I have a function:

scope.changeDate = function() {
    var current_date   = scope.status.date;
    current_date.setDate(current_date.getDate() - 1);
    scope.status['date'] = current_date;
}

This results that indeed scope.status.date is updated, but in the input box it stays the same.

What is happening here?

georgeawg
  • 48,608
  • 13
  • 72
  • 95
WJA
  • 6,676
  • 16
  • 85
  • 152

2 Answers2

1

Construct a new Date:

$scope.changeDate = function() {
    var current_date = $scope.status.date;
    current_date.setDate(current_date.getDate() - 1);
    $scope.status.date = new Date(current_date);
};

This will trigger the $render() function of the ngModelController.

The DEMO

angular.module("app",[])
.controller("ctrl", function($scope) {
    $scope.status = {date: new Date()};

    $scope.changeDate = function() {
        var current_date = $scope.status.date;
        current_date.setDate(current_date.getDate() - 1);
        $scope.status.date = new Date(current_date);
    };
})
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="app" ng-controller="ctrl">
     <input class="form-control"  type="date" ng-model="status.date"
            ng-model-options="{debounce:200}">
     <br>
     <button ng-click="changeDate()">Change Date</button>

</body>

Would have never thought of this that this triggers the render(). May I ask how you found this in the documentations? What did you look for?

From the Docs:

$render();

Since ng-model does not do a deep watch, $render() is only invoked if the values of $modelValue and $viewValue are actually different from their previous values. If $modelValue or $viewValue are objects (rather than a string or number) then $render() will not be invoked if you only change a property on the objects.

AngularJS ngModelController API Reference - $render

For more information, see

Community
  • 1
  • 1
georgeawg
  • 48,608
  • 13
  • 72
  • 95
  • Interesting! Would have never thought of this that this triggers the render(). May I ask how you found this in the documentations? What did you look for? – WJA Sep 19 '19 at 07:47
-1

Please check a date format, date format should be "yyyy-mm-dd"

Raj
  • 39
  • 5
  • `ng-model` binds the value to inputs, `value` attribute not needed – Bill P Sep 18 '19 at 19:01
  • yes you are right now I have updated my answer and can you check your date format because HTML 5 accept yyyy-mm-dd format.@Bill p – Raj Sep 18 '19 at 19:09