即使在赋值后$scope变量也未定义
$scope variable undefined even after assignment
我的一个 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>
对我来说,这似乎应该有效 - 似乎很简单。
将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>
相关文章:
- $scope变量被视为字符串AngularJs
- $scope变量,ng隐藏/显示
- $Scope变量在HTML中不更新,尽管在控制器中不断更新
- $scope变量发生更改时未触发Angular$watch
- 将Scope变量作为指针发送,并在控制器外部对其进行更改
- 正在从$scope变量中提取值
- 无法使用angularjs重置$scope变量
- 在 ng-click 中设置$scope变量指令
- 在从websocket服务器推送消息后更新$scope变量
- AngularJS中存在与$scope变量相关的问题.UI中的值未更改
- JavaScript未更新$scope变量的一部分
- 如何使用AngularJS在ng init中传递scope变量
- 如何访问ng-init或ng-checked中服务回调函数中声明的$scope变量
- 在我的HTML中用ng-init初始化$scope变量时出现问题
- angular js$scope变量与字符串绑定
- 如何在ng-init中使用$scope变量
- $scope变量从一个函数传递到另一个函数
- 如何在javascript中填充Angularjs$scope变量
- 阻止$scope变量副本侦听其原始副本
- 我们如何在Angular JS中使用$Scope变量而不声明