如何在foreach中将数据传递给angularjs foreach

时间:2019-01-18 12:12:36

标签: javascript angularjs

我添加了我想循环的代码和json。 我使用foreach循环循环了$scope.data并推送到$scope.tempObj,它的工作正常。但是,当我尝试循环$scope.data.policyDocumentContentCollection并推送到$scope.tempObj.nested时,其获取控制台错误“无法读取未定义的属性'push'”。我能知道如何将第二个foreach数据推送到$scope.tempObj中的嵌套数组中。

$scope.data = [
   {
      "policyNo":"DBDP18S016696",
      "renewalNo":"001",
      "endorseNo":"001",
      "productDesc":"TBD",
      "policyDocumentContentCollection":[
         {
            "docType":"Credit Note1"
         },
         {
            "docType":"Tax Invoice1"
         }
      ]
   },
   {
      "policyNo":"DBDP18S016697",
      "renewalNo":"001",
      "endorseNo":"001",
      "productDesc":"TBD",
      "policyDocumentContentCollection":[
         {
            "docType":"Credit Note1"
         },
         {
            "docType":"Tax Invoice1"
         }
      ]
   },
   {
      "policyNo":"DBDP18S016698",
      "renewalNo":"001",
      "endorseNo":"001",
      "productDesc":"TBD",
      "policyDocumentContentCollection":[
         {
            "docType":"Credit Note1"
         },
         {
            "docType":"Tax Invoice1"
         }
      ]
   }
]



$scope.tempObj = [];

                    angular.forEach($scope.data, function (data) {
                        $scope.tempObj.push({
                            'id': data.policyNo,
                            'renewalNo': data.renewalNo,
                            'endorsementNo': data.endorseNo,
                            'product': data.productDesc,
                            'insuredName': data.holderName,
                            'periodOfInsurance': data.fromDate + ' to ' + data.toDate,
                            'nested': []
                        });
                        angular.forEach($scope.data.policyDocumentContentCollection, function (docs) {
                            $scope.tempObj.nested.push({
                                docName: docs.docType
                            })
                        });
                    });

2 个答案:

答案 0 :(得分:4)

您可以创建一个中间对象,以便将其推入当前元素内。现在您不能,因为您按下了不存在的$scope.tempObj.nested,因为$scope.tempObj[index].nested是一个数组,所以只有$scope.tempObj存在。

angular.forEach($scope.data, function(data) {
  const element = {
    id: data.policyNo,
    renewalNo: data.renewalNo,
    endorsementNo: data.endorseNo,
    product: data.productDesc,
    insuredName: data.holderName,
    periodOfInsurance: data.fromDate + " to " + data.toDate,
    nested: []
  };

  angular.forEach(data.policyDocumentContentCollection, function(docs) {
    element.nested.push({
      docName: docs.docType
    });
  });

  $scope.tempObj.push(element);
});

答案 1 :(得分:0)

$scope.tempObj.nested不存在,因为$scope.tempObj是对象数组。因此,您必须首先在index数组的特定$scope.tempObj处访问对象,然后将值压入$scope.tempObj[index].nested数组。

$scope.data = [
   {
      "policyNo":"DBDP18S016696",
      "renewalNo":"001",
      "endorseNo":"001",
      "productDesc":"TBD",
      "policyDocumentContentCollection":[
         {
            "docType":"Credit Note1"
         },
         {
            "docType":"Tax Invoice1"
         }
      ]
   },
   {
      "policyNo":"DBDP18S016697",
      "renewalNo":"001",
      "endorseNo":"001",
      "productDesc":"TBD",
      "policyDocumentContentCollection":[
         {
            "docType":"Credit Note1"
         },
         {
            "docType":"Tax Invoice1"
         }
      ]
   },
   {
      "policyNo":"DBDP18S016698",
      "renewalNo":"001",
      "endorseNo":"001",
      "productDesc":"TBD",
      "policyDocumentContentCollection":[
         {
            "docType":"Credit Note1"
         },
         {
            "docType":"Tax Invoice1"
         }
      ]
   }
]



$scope.tempObj = {};

                    angular.forEach($scope.data, function (data, index) {
                        $scope.tempObj = {
                            'id': data.policyNo,
                            'renewalNo': data.renewalNo,
                            'endorsementNo': data.endorseNo,
                            'product': data.productDesc,
                            'insuredName': data.holderName,
                            'periodOfInsurance': data.fromDate + ' to ' + data.toDate,
                            'nested': []
                        };
                        angular.forEach($scope.data.policyDocumentContentCollection, function (docs) {
                            $scope.tempObj[index].nested.push({
                                docName: docs.docType
                            })
                        });
                    });
j