AnaCS
AnaCS

Reputation: 1011

can't change assigned default value to input of type date angularjs

I have the following input

 <input type="date" ng-model="startDateInput" ng-change="dateConvert()" />

And I set a default value since I call a service on the load of the page and need to pass the date as a parameter

$scope.startDateInput = new Date(new Date().setFullYear(new Date().getFullYear() - 1));
console.log("$scope.startDateInput" + $scope.startDateInput)

But later on I want this value to be updated when the user touches the date input and selects a new date on the calendar that pops up. The thing is, my value is not changed here. stays the same as the default value.

$scope.dateConvert = function() {
  var dateFrom = $scope.startDateInput;
  $scope.params.startDate = dateFrom;
  console.log("dateFrom " + dateFrom);
  console.log(" $scope.params.startDate  " + $scope.params.startDate);
}

the results of the log

dateFrom Wed Sep 13 2017 11:22:26 GMT+0100 (Western European Summer Time)
tab.accountStatement.controller.js:261 $scope.params.startDate Wed Sep 13 2017 11:22:26 GMT+0100 (Western European Summer Time)

It keeps the default value I assigned before, but why? How can I change this value?

Upvotes: 0

Views: 682

Answers (1)

Raju Sah
Raju Sah

Reputation: 600

angular.module('MyApp', [])
.controller('MyCtrl', function($scope,$filter){
  $scope.default_date =$filter("date")(Date.now(), 'yyyy-MM-dd');
  
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js"></script>
<div ng-app="MyApp">
  <div ng-controller="MyCtrl">
  <input type="date" ng-model="default_date" value="{{default_date}}">
  <p>{{default_date}}</p>
  </div>
</div>

Upvotes: 2

Related Questions