检查ng

编程入门 行业动态 更新时间:2024-10-28 05:15:56
检查ng-repeat内的项目是否已包含值(check if items inside ng-repeat already contains value)

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

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

I have JSON string of question and answer which binds in ng-repeat , now problem is i wants to show question once and all multiple answer within ng-repeat . this are my data.

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

view i am showing in

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

but this binds all questions multiple times with answer like

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

but i need like

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

i will be very thankful if anyone can help me with this

最满意答案

尝试这个,

将阵列中的'HrqQuestions'更改 使用'id'分组您的问题 并重复分组问题的 。 喜欢这个,

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> 
  
 

Try this,

Change your 'HrqQuestions' as array Group your questions using 'id' And repeat values of grouped questions. Like this,

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> 
  
 

更多推荐

本文发布于:2023-07-28 22:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1309792.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ng

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!