簡體   English   中英

在angularjs中調用json數組

[英]calling a json array in angularjs

我有一個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我出錯了,但是找不到,所以如何正確調用數組。 因此,在此尋求幫助。

我不知道您的數組是什么樣子,但是嘗試調用:

$scope.selectTeam[0].chelsea

由於未初始化,數組即將到來。 如果您初始化$ rootScope.teamAvailable = {}; 那么如果響應數據為json格式,它將被覆蓋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM