AngularJS JSON foreach值问题

时间:2015-08-13 12:29:42

标签: json angularjs parse-platform

我目前正在开发AngularJS / Parse.com网络应用程序

我想要实现的目标:
我想展示每个问题_4_p1'来自Parse.com JSON数据的值。

当前问题:
我已设法使用foreach函数显示每个问题_4_p1'值,但由于某种原因,它将每个值拆分为多个值 - 逐个字母。

enter image description here

任何帮助/建议都会有所帮助!

HTML:

<md-card>
  <md-card-content ng-controller="parseData">
    {{parseRecommendations | json}}
  </md-card-content>
</md-card>

控制器JS:

dashApp.controller("parseData", function ($scope, $http) {
  $scope.parseRecommendations = [];
  $http({
      method : 'GET',url : 'https://api.parse.com/1/classes/Customers',
      headers: { 'X-Parse-Application-Id':'xxx', 'X-Parse-REST-API-Key':'xxx'}
  })
    .success(function(data, error) {
      $scope.parseResults = data.results;
      angular.forEach($scope.parseResults, function(results) {
        angular.forEach(results.question_4_p1, function(value) {
          $scope.parseRecommendations.push(value);
        });
      });
    })
    .error(function(data, error) {alert('Failed, error code: ' + error.message);
  });
});

JSON数据:

{
    "results": [
        {
            "createdAt": "2015-08-07T14:28:41.416Z",
            "objectId": "oogpD4GjHM",
            "question_1_p1": 7,
            "question_1_p2": 5,
            "question_2_p1": 7,
            "question_2_p2": 3,
            "question_2_p3": "jane test",
            "question_2_p4": "jane test",
            "question_3_p1": 6,
            "question_4_p1": "yes",
            "question_4_p2": "jane test",
            "question_5_p1": 10,
            "question_6_p1": true,
            "question_6_p2": false,
            "question_6_p3": true,
            "question_6_p4": false,
            "question_6_p5": true,
            "question_7_p1": "jane test",
            "question_7_p2": "jane test",
            "question_8_p1": "jane test",
            "question_8_p2": "jane test",
            "updatedAt": "2015-08-07T14:28:41.416Z"
        },
        {
            "createdAt": "2015-08-07T15:13:56.399Z",
            "objectId": "2rEDCsAQgv",
            "question_1_p1": 7,
            "question_1_p2": 7,
            "question_2_p1": 6,
            "question_2_p2": 9,
            "question_3_p1": 8,
            "question_3_p2": "ASDASDSADASD",
            "question_3_p3": "ASDASDASDASDASDAS",
            "question_4_p1": "yes",
            "question_5_p1": 6,
            "question_6_p1": false,
            "question_6_p2": true,
            "question_6_p3": true,
            "question_6_p4": true,
            "question_6_p5": false,
            "question_7_p1": "ASDASDSADSAD",
            "question_7_p2": "ASDASDASDASD",
            "question_8_p1": "ADSADSADAS",
            "question_8_p2": "DSADASDASD",
            "updatedAt": "2015-08-07T15:13:56.399Z"
        },
        {
            "createdAt": "2015-08-07T15:14:20.398Z",
            "objectId": "GM9tcGcxAU",
            "question_1_p1": 2,
            "question_1_p2": 8,
            "question_2_p1": 3,
            "question_2_p2": 8,
            "question_3_p1": 8,
            "question_4_p1": "yes",
            "question_4_p2": "asdasda",
            "question_5_p1": 5,
            "question_6_p1": false,
            "question_6_p2": true,
            "question_6_p3": true,
            "question_6_p4": true,
            "question_6_p5": true,
            "question_7_p1": "asdasda",
            "question_7_p2": "asdasdad",
            "question_8_p1": "dfsdfsdf",
            "question_8_p2": "sdfsdfs",
            "updatedAt": "2015-08-07T15:14:20.398Z"
        },
        {
            "createdAt": "2015-08-07T15:14:22.331Z",
            "objectId": "jFVCcrJr4O",
            "question_1_p1": 9,
            "question_1_p2": 9,
            "question_2_p1": 8,
            "question_2_p2": 8,
            "question_3_p1": 8,
            "question_4_p1": "yes",
            "question_4_p2": "Because of Meg.",
            "question_5_p1": 8,
            "question_6_p1": true,
            "question_6_p2": true,
            "question_6_p3": true,
            "question_6_p4": true,
            "question_6_p5": true,
            "question_7_p1": "Meg's voice on the phone.",
            "question_7_p2": "Meg's skills.",
            "question_8_p1": "Need more Meg.",
            "question_8_p2": "Promote Meg.",
            "updatedAt": "2015-08-07T15:14:22.331Z"
        },
        {
            "createdAt": "2015-08-07T15:15:35.551Z",
            "objectId": "4EJUcho22X",
            "question_1_p1": 7,
            "question_1_p2": 3,
            "question_2_p1": 10,
            "question_2_p2": 2,
            "question_2_p3": "jane again",
            "question_2_p4": "jane again",
            "question_3_p1": 2,
            "question_3_p2": "jane again",
            "question_3_p3": "jane again",
            "question_4_p1": "no",
            "question_4_p2": "jane again",
            "question_5_p1": 6,
            "question_6_p1": true,
            "question_6_p2": true,
            "question_6_p3": true,
            "question_6_p4": true,
            "question_6_p5": false,
            "question_7_p1": "jane again",
            "question_7_p2": "jane again",
            "question_8_p1": "jane again",
            "question_8_p2": "jane again",
            "updatedAt": "2015-08-07T15:15:35.551Z"
        },
        {
            "createdAt": "2015-08-10T08:35:17.329Z",
            "objectId": "sNxWquVrVW",
            "question_1_p1": 9,
            "question_1_p2": 7,
            "question_2_p1": 9,
            "question_2_p2": 7,
            "question_3_p1": 8,
            "question_4_p1": "no",
            "question_4_p2": "fdgdf",
            "question_5_p1": 8,
            "question_6_p1": false,
            "question_6_p2": true,
            "question_6_p3": true,
            "question_6_p4": false,
            "question_6_p5": true,
            "question_7_p1": "dsvfxf",
            "question_7_p2": "vfdv",
            "question_8_p1": "sdvsdv",
            "question_8_p2": "sdfv",
            "updatedAt": "2015-08-10T08:35:17.329Z"
        },
        {
            "createdAt": "2015-08-10T11:44:17.142Z",
            "objectId": "pQnE278xOH",
            "question_1_p1": 10,
            "question_1_p2": 3,
            "question_2_p1": 9,
            "question_2_p2": 6,
            "question_3_p1": 7,
            "question_4_p1": "yes",
            "question_4_p2": "Cheeses",
            "question_5_p1": 6,
            "question_6_p1": false,
            "question_6_p2": true,
            "question_6_p3": true,
            "question_6_p4": false,
            "question_6_p5": false,
            "question_7_p1": "Adam",
            "question_7_p2": "Adam",
            "question_8_p1": "Tea",
            "question_8_p2": "Chips",
            "updatedAt": "2015-08-10T11:44:17.142Z"
        },
        {
            "createdAt": "2015-08-10T14:00:15.051Z",
            "objectId": "1Kq7Hs7gDM",
            "question_1_p1": 5,
            "question_1_p2": 9,
            "question_2_p1": 6,
            "question_2_p2": 8,
            "question_3_p1": 6,
            "question_4_p1": "yes",
            "question_5_p1": 8,
            "question_6_p1": true,
            "question_6_p2": true,
            "question_6_p3": true,
            "question_6_p4": true,
            "question_6_p5": true,
            "question_7_p1": "fish",
            "question_7_p2": "and chips",
            "question_8_p1": "gravy",
            "question_8_p2": "and pies",
            "updatedAt": "2015-08-10T14:00:15.051Z"
        },
        {
            "createdAt": "2015-08-11T11:06:41.791Z",
            "objectId": "NXA33lXq9V",
            "question_1_p1": 3,
            "question_1_p2": 8,
            "question_2_p1": 7,
            "question_2_p2": 8,
            "question_3_p1": 5,
            "question_4_p1": "yes",
            "question_5_p1": 7,
            "question_6_p1": false,
            "question_6_p2": false,
            "question_6_p3": false,
            "question_6_p4": false,
            "question_6_p5": true,
            "question_7_p1": "ddd",
            "question_7_p2": "ddd",
            "question_8_p1": "ddd",
            "question_8_p2": "ddd",
            "updatedAt": "2015-08-11T11:06:41.791Z"
        },
        {
            "createdAt": "2015-08-11T13:28:47.668Z",
            "objectId": "LnOnfVZ1EW",
            "question_1_p1": 4,
            "question_1_p2": 9,
            "question_2_p1": 2,
            "question_2_p2": 3,
            "question_2_p3": "Shit",
            "question_2_p4": "Fuck",
            "question_3_p1": 5,
            "question_3_p2": "Adam",
            "question_3_p3": "Akdjfj",
            "question_4_p1": "yes",
            "question_4_p2": "Dhdj",
            "question_5_p1": 5,
            "question_6_p1": true,
            "question_6_p2": false,
            "question_6_p3": true,
            "question_6_p4": false,
            "question_6_p5": true,
            "question_7_p1": "Dkdjdn",
            "question_7_p2": "Shsbsb",
            "question_8_p1": "Jdjddj",
            "question_8_p2": "Ndndnd",
            "updatedAt": "2015-08-11T13:28:47.668Z"
        },
        {
            "createdAt": "2015-08-12T16:22:01.360Z",
            "objectId": "Ip8H9r45r3",
            "question_1_p1": 7,
            "question_1_p2": 5,
            "question_2_p1": 7,
            "question_2_p2": 6,
            "question_3_p1": 7,
            "question_4_p1": "no",
            "question_4_p2": "asdad",
            "question_5_p1": 7,
            "question_6_p1": true,
            "question_6_p2": true,
            "question_6_p3": true,
            "question_6_p4": false,
            "question_6_p5": true,
            "question_7_p1": "asdasdas",
            "question_7_p2": "asdasdas",
            "question_8_p1": "asdadas",
            "question_8_p2": "asdasdasd",
            "updatedAt": "2015-08-12T16:22:01.360Z"
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

您没有正确循环。

$scope.parseResults = data.results; // your results array
  // for each result...
  angular.forEach($scope.parseResults, function(results) { 
    // loop over the question value?
    angular.forEach(results.question_4_p1, function(value) { 
      $scope.parseRecommendations.push(value);
    });
  });
});

您在第一次迭代时循环$scope.parseResults[0].question_4_p1,而您正在循环的值是“是”&#39;。

当你循环一个字符串时,你会循环遍历它的字符。

对于每一行,你都会在同一个问题上循环,所以你最终得到的是“是的,是的,是的,还有是等等。”

你需要失去内循环,你会得到你想要的东西:

$scope.parseResults = data.results; // your results array
  angular.forEach($scope.parseResults, function(results) {
    $scope.parseRecommendations.push(results.question_4_p1);
  });
});

这会留给您['yes', 'no', 'yes', 'yes', 'yes']等。