检查ng-repeat中的项目是否已包含值

时间:2016-08-19 10:17:38

标签: javascript angularjs angularjs-ng-repeat angular-ng-if

我有一个问题和答案的JSON字符串,它绑定在ng-repeat中, 现在问题是我想在ng-repeat中显示一次问题和所有多个答案。 这是我的数据。

{Answer:"White",AnswerID:967,answer_type:"RADIO",fullquestion:"Your Race",id:6}{Answer:"African American",AnswerID:968,answer_type:"RADIO",fullquestion:"Your Race",id:6}{Answer:"Asian",AnswerID:969,answer_type:"RADIO",fullquestion:"Your Race",id:6}

我在

中显示的视图
 <div ng-repeat="hrq in HrqQuestions">
            <div class="list card normal">
                <div class="item item-body item-divider">                    
                    <p ng-bind="hrq.fullquestion"></p>                       
                </div>
                <label class="item item-input" ng-show="hrq.answer_type=='FREETEXT'">
                    <input ng-model="hrq.Answer" name="name" type="text">
                </label>
                <div ng-if="hrq.answer_type=='RADIO'">
                    <ion-radio ng-click="selectedAnswer(hrq.AnswerID,hrq.Answer)" name="radio1" ng-value="hrq.AnswerID">{{hrq.Answer}}</ion-radio>
                </div>
            </div>
        </div>

但这会多次将所有问题与

等答案联系起来
     Q.Your Race
           White Your Race
     Q.Your Race
           African American Your Race
     Q.Your Race
           Asian

但我需要

     Q. Your Race
           White Your Race
           African American Your Race
           Asian

如果有人能帮助我,我将非常感激

2 个答案:

答案 0 :(得分:3)

尝试this fiddle

    <div ng-controller="MyCtrl">      
  <div ng-repeat="hrq in HrqQuestions">
            <div class="list card normal">
                <div class="item item-body item-divider">                    
                  <p>{{$index + 1}}. {{hrq.fullquestion}}</p>              
                </div>
                <div ng-repeat="answer in hrq.answers">
                  <label class="item item-input" ng-show="answer.answer_type=='FREETEXT'">
                    <input ng-model="answer.Answer" name="name" type="text">
                </label>
                <div ng-if="answer.answer_type=='RADIO'">
                    <input type="radio" ng-click="selectedAnswer(answer.AnswerID,answer.Answer)" name="radio1" ng-value="answer.AnswerID">{{answer.Answer}} 
                </div>
                </div>
                <br>
            </div>
        </div>
</div>

控制器代码

    $scope.HrqQuestions = [{ fullquestion: "Your Race", id: 6,
                         answers: [{ Answer: "White", AnswerID: 967, answer_type: "RADIO" },
                                   { Answer: "African American", AnswerID: 968, answer_type: "RADIO" },
                                   { Answer: "Asian", AnswerID: 969, answer_type: "RADIO" }] }, 
                        { fullquestion: "My Race", id: 7, 
                          answers: [{ Answer: "Black", AnswerID: 967, answer_type: "RADIO" },
                                    { Answer: "African Indian", AnswerID: 968, answer_type: "RADIO" },
                                    { Answer: "India", AnswerID: 969, answer_type: "RADIO" }] }];

答案 1 :(得分:3)

试试这个,

  1. 'HrqQuestions' 更改为数组
  2. 使用 'id'
  3. 对您的问题进行分组
  4. 并重复分组问题的 。像这样,
  5. angular.module('app',['angular.filter']).controller('MainController', function($scope) {
      $scope.HrqQuestions = [
          {Answer:"White",AnswerID:967,answer_type:"RADIO",fullquestion:"Your Race",id:6},
          {Answer:"African American",AnswerID:968,answer_type:"RADIO",fullquestion:"Your Race",id:6},
          {Answer:"Asian",AnswerID:969,answer_type:"RADIO",fullquestion:"Your Race",id:6}
        ];
     });
    <!DOCTYPE html>
    <html ng-app="app">
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
      <script src="//cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.4/angular-filter.js"></script>
    
    <body ng-controller="MainController">
      <div ng-repeat="(key, value) in HrqQuestions | groupBy: 'id'">
        <div><strong>Id:</strong> {{ key }}</div>
         <p ng-repeat="v in value">
           <strong>Answer {{ $index + 1}}</strong>. {{ v.Answer }}
         </p>
      </div>
    </body>
    </html>