Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript $scope变量未定义,即使在赋值之后_Javascript_Angularjs - Fatal编程技术网

Javascript $scope变量未定义,即使在赋值之后

Javascript $scope变量未定义,即使在赋值之后,javascript,angularjs,Javascript,Angularjs,我的一个角度控制器有功能,我有一个问题 在函数开始时,我尝试检查是否提供了出发时间,如果没有,我将以HH:mm:ss格式为其分配当前时间 但是,当我尝试检查$scope.selectedDepartureTime.departureTime的值时,即使在赋值之后,它仍然是未定义的。为什么? $scope.search = function () { if (!$scope.selectedDepartureTime.departureTime) { var time =

我的一个角度控制器有功能,我有一个问题

在函数开始时,我尝试检查是否提供了出发时间,如果没有,我将以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>
在以下时间后离开:

对我来说,这似乎应该是可行的-似乎很简单。

未定义的
更改为
{}

<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>
在以下时间后离开:

仅凭您发布的信息很难判断出问题所在。第80行和第85行是什么?第80行和第85行是console.log语句。您在其中初始化了$scope.SelectedDepartTime?是否在为对象分配属性之前创建对象?而不是ng init=“SelectedDepartTime='undefined'”,更改为ng init=“SelectedDepartTime='{}'”,或者从其中删除并在控制器中初始化