2016-08-19 28 views
8

Mam ciąg znaków JSON z pytaniem i odpowiedzią, która wiąże się z ng-repeat, teraz problem polega na tym, że chcę pokazać pytanie raz i wszystkie wielokrotne odpowiedzi w ciągu ng-repeat. to są moje dane.Sprawdź, czy elementy wewnątrz ng-repeat już zawierają wartość

{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} 

widok przytaczam w

<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> 

ale to wiąże wszystkie pytania wiele razy odpowiedź jak

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

ale muszę jak

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

będę mieć bardzo wdzięczny, jeśli ktoś może mi pomóc dowcip h to

+0

jeden pomysłem byłoby przekształcić HrqQuestions obiekt do formatu chcesz. – Ruhul

Odpowiedz

3

spróbować,

  1. zmienić 'HrqQuestions' jako tablicy
  2. Grupa twoje pytania wykorzystujące 'id'
  3. i powtórzyć wartości zgrupowanych pytania. Tak,

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>

+0

Wspaniale, dziękuję bardzo za to, czego szukałem :) – Vikas

3

Spróbuj jak 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> 

kodzie kontrolera

$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" }] }]; 
+0

więcej niż jedno pytanie jest w ciągu JSON, więc muszę użyć go wewnątrz ng-repeat, aby pokazać wszystkie pytania, nie mogę pokazać ich indywidualnie. – Vikas

+0

ok, zmieniając moją odpowiedź –

+0

Mam zaktualizowaną odpowiedź, musisz zmienić strukturę JSON, Zachowaj pytanie i odpowiedzi w obiekcie. zobacz zaktualizowaną odpowiedź i skrzypce –