2016-04-07 29 views
0

私の目的は(テーブルの行として)オブジェクトから配列を作成することですが、私は$ rootScope:infdigエラーを取得します。私は同様の質問here見てきましたが、それは助けにはならなかった

$scope.rows = { 
"TableName": "TestTable", 
"Rows": [{ 
    "Name": "Name1", 
    "RowCells": [{ 
     "Y": 2001, 
     "M": 164 
    }, { 
     "Y": 2002, 
     "M": 178 
    }, { 
     "Y": 2003, 
     "M": 188 
    }, { 
     "Y": 2007, 
     "M": 295 
    }, { 
     "Y": 2008, 
     "M": 316 
    }, { 
     "Y": 2009, 
     "M": 328 
    }] 
}], 
"Rows": [{ 
    "Name": "Name2", 
    "RowCells": [{ 
     "Y": 2001, 
     "M": 164 
    }, { 
     "Y": 2002, 
     "M": 178 
    }, { 
     "Y": 2003, 
     "M": 188 
    }, { 
     "Y": 2007, 
     "M": 295 
    }, { 
     "Y": 2008, 
     "M": 316 
    }, { 
     "Y": 2009, 
     "M": 328 
    }] 
}], 
"Rows": [{ 
    "Name": "Name3", 
    "RowCells": [{ 
     "Y": 2001, 
     "M": 164 
    }, { 
     "Y": 2002, 
     "M": 178 
    }, { 
     "Y": 2003, 
     "M": 188 
    }, { 
     "Y": 2007, 
     "M": 295 
    }, { 
     "Y": 2008, 
     "M": 316 
    }, { 
     "Y": 2009, 
     "M": 328 
    }] 
}] 
} 

$scope.rowCells = function() { 
       var tableRowCollection = []; 

       angular.forEach($scope.rows, function (item) { 
        var tableRow = []; 
        tableRow.push(item.Name); 
        angular.forEach(item.RowCells, function (i) { 
         tableRow.push(i.M); 
        }); 
        tableRowCollection.push(tableRow); 
       }); 
       return tableRowCollection ; 
      }; 

<div data-ng-repeat="r in rowCells()">{{r}}</div> 

は、ここに私のコードです。 私は何が不足していますか、どこが間違っていますか?

答えて

0

あなたの行の配列は次のようにする必要があります:

$scope.rows = { 
"TableName": "TestTable", 
"Rows": [{ 
    "Name": "Name1", 
    "RowCells": [{ 
     "Y": 2001, 
     "M": 164 
    }, { 
     "Y": 2002, 
     "M": 178 
    }, { 
     "Y": 2003, 
     "M": 188 
    }, { 
     "Y": 2007, 
     "M": 295 
    }, { 
     "Y": 2008, 
     "M": 316 
    }, { 
     "Y": 2009, 
     "M": 328 
    }] 
}, { 
    "Name": "Name2", 
    "RowCells": [{ 
     "Y": 2001, 
     "M": 164 
    }, { 
     "Y": 2002, 
     "M": 178 
    }, { 
     "Y": 2003, 
     "M": 188 
    }, { 
     "Y": 2007, 
     "M": 295 
    }, { 
     "Y": 2008, 
     "M": 316 
    }, { 
     "Y": 2009, 
     "M": 328 
    }] 
}, { 
    "Name": "Name3", 
    "RowCells": [{ 
     "Y": 2001, 
     "M": 164 
    }, { 
     "Y": 2002, 
     "M": 178 
    }, { 
     "Y": 2003, 
     "M": 188 
    }, { 
     "Y": 2007, 
     "M": 295 
    }, { 
     "Y": 2008, 
     "M": 316 
    }, { 
     "Y": 2009, 
     "M": 328 
    }] 
}] 
} 
+0

あなたは正しいです、私のオブジェクトに誤りがありましたが、私は例を作成している間に、これはのみで滑っている、実際のオブジェクトはokです。 – agri

0

Staskusが示唆したように、あなたはあなたの配列を変更する必要があります。あなたのforeachループは以下のようにする必要があります:

angular.forEach($scope.rows.Rows, function(item) { 
    var tableRow = []; 
    tableRow.push(item.Name); 
    angular.forEach(item.RowCells, function(i) { 
     tableRow.push(i.M); 
    }); 
    tableRowCollection.push(tableRow); 
}); 
+0

はい、この例では、$ scope.rowsは$ scope.rows.Rowsであったはずです。私が実際に使っているオブジェクトは、apiからajax経由で入ってきて、$ scope.rows = response.data.Rowsとして設定されています。私はまだError:[$ rootScope:infdig] – agri

+0

$ rootScope:infdigエラーは、モデルがビューデータに基づいて更新され、ビューデータがモデル更新を引き起こしている無限ループがある場合に発生します。あなたが提供したHTMLには部分的なものしか含まれていません。あなたは完全なHTMLを提供することができる場合、それは識別するのは簡単だろう。無限のエラー:https://docs.angularjs.org/error/$rootScope/infdig – Sree

+0

はい、私はそのリンクを見て、問題を引き起こしている部分が

{{r}}
だと分かりました。私は別のアプローチを取って終わった。 – agri

関連する問題