代码之家  ›  专栏  ›  技术社区  ›  Mike

无法读取下拉列表中未定义的属性“id”-AngularJS

  •  0
  • Mike  · 技术社区  · 6 年前

    我填充了一个动态下拉列表,如下所示:

    $scope.idSelectionModel = [];
    $http({
            'url': '/getIds',
            'method': 'POST',
            'headers': {
                'Content-Type': 'application/json'
            }
        })
        .then(
            function(
                response) {
                $scope.idResponse = response.data;
    
                angular.forEach($scope.idResponse, function(value, key) {
                    $scope.idSelectionModel.push({
                        id: value
                    });
                });
            });
    
    $scope.identity = $scope.idSelectionModel[0];
    
    console.log($scope.idSelectionModel);
    

    $scope.idResponse 返回一个列表,我对它做了一些修改,将不同的id映射到list元素。但我注意到有三种行为:

    1. 这个 console.log
    2. 当我尝试打印时 $scope.identity.id ,我得到一个异常“无法读取dropdown中未定义的属性'id'”。

    有人能指导我吗?

    0 回复  |  直到 6 年前