Im trying to pass the date from a date picker value into a $http session parameter (like "2017-12-7"), but the date my datepicker Date variable is 'undefined'
angular.module('MyApp', ['ngMaterial']).controller('MyController',
function($scope, $http) {
this.myDate = new Date();
this.minDate = new Date("December 4, 2017");
this.maxDate = new Date("December 10, 2017");
$scope.getDataFromServer = function() {
var dt = this.myDate;
$http({
method : 'GET',
url : 'myurl',
params : {
pdate : this.myDate
}
}).then(function mySuccess(response) {
$scope.datacontainer= response.data;
}, function myError(response) {
});
};
});
The $scope.getDataFromServer method is called when you click a button. The following line var dt = this.myDate; was to look at the myDate object in chrome DevTools (F12) and it says 'undefined'.
The date picker HTML is as follows:
<div flex="20">
<md-datepicker ng-model="myDate" md-placeholder="Enter date" md-min-date="ctrl.minDate" md-max-date="ctrl.maxDate">
</md-datepicker>
</div>
var myContext = this;and getmyContext.myDateMyApp as ctrlin the view, so you need to set the modal like this:ng-model="ctrl.myDate". In the controller itself, put on the very top line of the controller function this:var ctrl = this;and then change all the currentthisand$scopetoctrlso your code will be more readable. Don't forget the add thectrl.prefix for any other controller properties in the VIEW