-1
我在我的一個Angular控制器中有函數,我遇到了一個問題。
在函數的開始處,我嘗試檢查是否提供了出發時間,如果沒有提供出發時間,我以HH:mm:ss格式爲其分配當前時間。
但是,當我嘗試檢查值$ scope.selectedDepartureTime.departureTime甚至在賦值後,它仍然是未定義的。爲什麼?
$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);
//12:32:15 app.js:80
console.log(departureTime);
$scope.selectedDepartureTime.departureTime =
departureTime;
//undefined app.js:85
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;
});
}
數據從一個HTML表單提交:
<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>
對我來說,這似乎像它應該工作 - 似乎很簡單。
很難判斷您發佈的信息有什麼問題。 80和85線是什麼? – Chanthu
第80行和第85行是console.log語句。 – moka
你在哪裏初始化$ scope.selectedDepartureTime? – Ricky