在angularjs中调用json数组

时间:2016-12-13 11:58:08

标签: javascript angularjs arrays json

我有一个

的json数组
[{
"id": "21390238becde1290",
"chelsea": {
    "homeTeam": "chelsea",
    "sortName": "ches",
    "awayTeam": "Maribor",
    "awayShort": ""
},
"barclays": {
    "id": "21390238becde1290",
    "homeTeam": "barclays",
    "sortName": "barc",
    "awayTeam": "Hull",
    "awayShort": ""
}
}]

控制器:

footBallApp.controller('TeamInfoEdit',
    function ($rootScope, $scope, $state, $translate, carwashService) {

        $rootScope.washTypes;

        $scope.onViewLoaded = function () {
           matchService.getTeamTypes($scope.TypeSelected);
        }

        $scope.TypeSelected = function (response) {
            debugger;
            if (response) {
                $rootScope.teamAvailable = response;
                $scope.localizedTeamName = getLocalizedCollection($scope.teamAvailable);
            }
        }

        // Editing the Team Details.

        $scope.editTeamDetails = function (key) {
            console.log(key._id);

            $rootScope.selectTeam =[]; 
            for(var i=0; i<$scope.teamAvailable.length;i++){
            if($scope.teamAvailable[i]._id== key._id)
            {
                console.log($scope.teamAvailable[i]);
                $scope.selectTeam.push($scope.teamAvailable[i]);
                debugger;

我已将数组分配给$scope.selectTeam。当我试图呼叫$scope.selectTeam.chelsea时,我在控制台中将其作为Undefined。在调用json我出错但无法找到它的同时,我该如何正确调用数组。所以寻求帮助。

2 个答案:

答案 0 :(得分:2)

我不知道您的阵列是什么样的,但请尝试致电:

$scope.selectTeam[0].chelsea

答案 1 :(得分:0)

由于未初始化,数组即将到来。 如果你初始化$ rootScope.teamAvailable = {};然后,如果响应数据以json格式出现,它将覆盖。