I am facing an issue with a function in one of my Angular controllers.
Within the function, I check if a departure time has been provided. If it hasn't, I assign the current time to it in a HH:mm:ss format.
Even after assigning a value to $scope.selectedDepartureTime.departureTime, it still shows as undefined when I try to check its value. Why is this happening?
$scope.search = function () {
if (!$scope.selectedDepartureTime.departureTime) {
var time = new Date();
var departureTime =
("0" + time.getHours()).slice(-2) + ":" +
("0" + time.getMinutes()).slice(-2) + ":" +
("0" + time.getSeconds()).slice(-2);
console.log(departureTime);
$scope.selectedDepartureTime.departureTime =
departureTime;
console.log($scope.selectedDepartureTime.departureTime);
}
$http({
method: 'GET',
url: '/train-times/journey?departureStation='
+ $scope.selectedDepartureStation.stationName
+ '&destinationStation='
+ $scope.selectedDestinationStation.stationName
+ '&queryTime='
+ $scope.selectedDepartureTime.departureTime,
data: {}
}).success(function (result) {
$scope.journeyResult = result;
});
$http({
method: 'GET',
url: '/train-times/departure-times?departureStation='
+ $scope.selectedDepartureStation.stationName
+ '&queryTime='
+ $scope.selectedDepartureTime.departureTime,
data: {}
}).success(function (result) {
$scope.otherDepartureTimes = result;
});
}
The data is submitted through an HTML form:
<label for="departAfter">Depart After:</label>
<select id="departAfter"
ng-model="selectedDepartureTime"
ng-options="time.departureTime for time in departureTimes"
ng-init="selectedDepartureTime='undefined'">
</select>
It seems like this should work and is a simple enough process.