2017-07-03 62 views
1

我有我的对象的对象,但NG重复数据没有显示我什么我的数据是JSON格式,如:NG重复不使用对象的对象工作

 { 
     "0": [ 
     { 
      "question": "How often is real property re-assessed (or revalued)?", 
      "id": 1, 
      "section": "Assessment", 
      "sectionId": 2, 
      "check": true, 
      "index": 0 
     }, 
{ 
      "question": "How often is real property re-assessed (or revalued)?", 
      "id": 1, 
      "section": "Assessment", 
      "sectionId": 2, 
      "check": true, 
      "index": 0 
     }, 
     { 
      "key": "Survey Meta Data" 
     } 
     ], 
     "1": [ 
     { 
      "question": "When are New Assessment Notices sent out?", 
      "id": 2, 
      "section": "Assessment", 
      "sectionId": 2, 
      "check": true, 
      "index": 1 
     }, 
     { 
      "key": "Assessment" 
     } 
     ] 
    } 

,我想展示所有的问题,关键我怎么能做到这一点我想是这样的:

<div class="form-group" ng-repeat="data in viewQuestions"> 
        <div ng-repeat="values[$index] in data "> 
        <label for="comment">{{values.questions}}</label> 
        </div> 
        <label for="comment">{{data.key}}</label> 
        <textarea class="form-control" rows="2" id="comment"></textarea> 
        </div> 
+0

你如何看待输出? – Sajeetharan

+0

我想先查看问题,然后输入如下内容: –

+0

问题:房地产重估(或重估)的频率如何? 问题:“房地产重估(或重估)的频率如何? 关键:调查元数据 问题:何时发出新评估通知 关键:评估 –

回答

1

删除$index在NG-重复。

也改变{{values.questions}}{{values.question}}

<div class="form-group" ng-repeat="data in viewQuestions"> 
    <div ng-repeat="values in data "> 
     <label >{{values.question}}</label> 
    </div> 
    <label for="comment">{{data.key}}</label> 
    <textarea class="form-control" rows="2" id="comment"></textarea> 
</div> 

演示

angular.module("app",[]) 
 
.controller("ctrl",function($scope){ 
 
    $scope.viewQuestions = { 
 
     "0": [ 
 
     { 
 
      "question": "How often is real property re-assessed (or revalued)?", 
 
      "id": 1, 
 
      "section": "Assessment", 
 
      "sectionId": 2, 
 
      "check": true, 
 
      "index": 0 
 
     }, 
 
{ 
 
      "question": "How often is real property re-assessed (or revalued)?", 
 
      "id": 1, 
 
      "section": "Assessment", 
 
      "sectionId": 2, 
 
      "check": true, 
 
      "index": 0 
 
     }, 
 
     { 
 
      "key": "Survey Meta Data" 
 
     } 
 
     ], 
 
     "1": [ 
 
     { 
 
      "question": "When are New Assessment Notices sent out?", 
 
      "id": 2, 
 
      "section": "Assessment", 
 
      "sectionId": 2, 
 
      "check": true, 
 
      "index": 1 
 
     }, 
 
     { 
 
      "key": "Assessment" 
 
     } 
 
     ] 
 
    } 
 

 
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="app" ng-controller="ctrl"> 
 
<div class="form-group" ng-repeat="data in viewQuestions"> 
 
        <div ng-repeat="values in data "> 
 
        <label >{{values.question}}</label> 
 
        </div> 
 
        <label for="comment">{{data.key}}</label> 
 
        <textarea class="form-control" rows="2" id="comment"></textarea> 
 
        </div> 
 
</div>

+0

很棒:)工作良好 –

2

Proably你需要这个,

<div class="form-group" ng-repeat="(key,value) in viewQuestions track by $index"> 
    <div ng-repeat="values in value "> 
    <label for="comment">{{values.question}}</label> 
    <label for="comment">{{values.key}}</label> 
    <textarea class="form-control" rows="2" id="comment"></textarea> 
</div> 
</div> 

DEMO

var pegasusWebApp = angular.module('ReqWebApp', []) 
 

 
pegasusWebApp.controller('ReqAppController', function ReqAppController($scope) { 
 
    $scope.viewQuestions = { 
 
    "0": [ 
 
    { 
 
     "question": "How often is real property re-assessed (or revalued)?", 
 
     "id": 1, 
 
     "section": "Assessment", 
 
     "sectionId": 2, 
 
     "check": true, 
 
     "index": 0 
 
    }, 
 
    { 
 
     "question": "How often is real property re-assessed (or revalued)?", 
 
     "id": 1, 
 
     "section": "Assessment", 
 
     "sectionId": 2, 
 
     "check": true, 
 
     "index": 0 
 
    }, 
 
    { 
 
     "key": "Survey Meta Data" 
 
    } 
 
    ], 
 
    "1": [ 
 
    { 
 
     "question": "When are New Assessment Notices sent out?", 
 
     "id": 2, 
 
     "section": "Assessment", 
 
     "sectionId": 2, 
 
     "check": true, 
 
     "index": 1 
 
    }, 
 
    { 
 
     "key": "Assessment" 
 
    } 
 
    ] 
 
}; 
 
});
<!DOCTYPE html> 
 
<html ng-app="ReqWebApp"> 
 
<head> 
 
    <meta charset="UTF-8"> 
 
    <title>New Request</title> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
</head> 
 
<body ng-controller="ReqAppController"> 
 
    <div class="form-group" ng-repeat="(key,value) in viewQuestions track by $index"> 
 
     <div ng-repeat="values in value "> 
 
      <label for="comment">{{values.question}}</label> 
 
      <label for="comment">{{values.key}}</label> 
 
      <textarea class="form-control" rows="2" id="comment"></textarea> 
 
     </div> 
 
    </div> 
 
</body> 
 
</html>

0

angular.module("Myapp",[]) 
 
.controller("Myctrl",function($scope){ 
 
$scope.viewQuestions = { 
 
     "0": [ 
 
     { 
 
      "question": "How often is real property re-assessed (or revalued)?", 
 
      "id": 1, 
 
      "section": "Assessment", 
 
      "sectionId": 2, 
 
      "check": true, 
 
      "index": 0 
 
     }, 
 
{ 
 
      "question": "How often is real property re-assessed (or revalued)?", 
 
      "id": 1, 
 
      "section": "Assessment", 
 
      "sectionId": 2, 
 
      "check": true, 
 
      "index": 0 
 
     }, 
 
     { 
 
      "key": "Survey Meta Data" 
 
     } 
 
     ], 
 
     "1": [ 
 
     { 
 
      "question": "When are New Assessment Notices sent out?", 
 
      "id": 2, 
 
      "section": "Assessment", 
 
      "sectionId": 2, 
 
      "check": true, 
 
      "index": 1 
 
     }, 
 
     { 
 
      "key": "Assessment" 
 
     } 
 
     ] 
 
    } 
 
    })
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
 
    <div ng-app="Myapp" ng-controller="Myctrl"> 
 
    <div class="form-group" ng-repeat="(key,val) in viewQuestions track by $index"> 
 
    <div ng-repeat="v in val "> 
 
     <label for="comment">{{v.question}}</label> 
 
    </div> 
 
    <label for="comment">{{val.key}}</label> 
 
    <textarea class="form-control" rows="2" id="comment">  </textarea> 
 
    </div> 
 
    </div>

0

这里是JsFiddle link

HTML

<div ng-app="myApp"> 

     <div ng-controller="ctrl"> 
     <div ng-repeat="obj in data"> 
      <div ng-repeat="item in obj"> 
      <label ng-if="item.question">Question:{{item.question}}</label> 
      <label ng-if="item.key">Key: {{item.key}}</label> 
      <br> 
      <textarea ng-if="item.key" class="form-control" rows="2" id="comment"> 
      </textarea> 
      <hr> 
      </div> 

     </div> 
     </div> 
    </div> 

JS代码

var app = angular.module('myApp', []); 

    app.controller('ctrl', function($scope) { 
    $scope.data = { 
     "0": [{ 
     "question": "How often is real property re-assessed (or revalued)?", 
     "id": 1, 
     "section": "Assessment", 
     "sectionId": 2, 
     "check": true, 
     "index": 0 
     }, { 
     "question": "How often is real property re-assessed (or revalued)?", 
     "id": 2, 
     "section": "Assessment", 
     "sectionId": 2, 
     "check": true, 
     "index": 0 
     }, { 
     "key": "Survey Meta Data" 
     }], 
     "1": [{ 
     "question": "When are New Assessment Notices sent out?", 
     "id": 2, 
     "section": "Assessment", 
     "sectionId": 2, 
     "check": true, 
     "index": 1 
     }, { 
     "key": "Assessment" 
     }] 
    }; 
    }); 

这将打印出你想要的。