[英]$scope variable undefined even after assignment
I have function in one of my Angular controllers and I'm having an issue with it.我的其中一个 Angular 控制器有功能,但我遇到了问题。
At the beginning of the function I try to check if a departure time as been provided, if one hasn't been provided, I assign the current time to it in a HH:mm:ss format.在函数开始时,我尝试检查是否提供了出发时间,如果没有提供,我会以 HH:mm:ss 格式为其分配当前时间。
However, when i try to check the value of $scope.selectedDepartureTime.departureTime even after assignment, it is still undefined.但是,当我尝试在赋值后检查 $scope.selectedDepartureTime.departureTime 的值时,它仍然是未定义的。 Why?为什么?
$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;
});
}
Data is being submitted from a HTML form:正在从 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>
To me, this seems like it should work - seems simple enough.对我来说,这似乎应该可行 - 似乎很简单。
Change undefined
to {}
.将undefined
更改为{}
。
<label for="departAfter">Depart after:</label>
<select id="departAfter"
ng-model="selectedDepartureTime"
ng-options="time.departureTime for time in departureTimes"
ng-init="selectedDepartureTime={}"> <!-- Change undefined to {}-->
</select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.