在两个独立的div中分割ng-repeat的最有效方法

时间:2017-06-15 08:52:55

标签: javascript html angularjs

我有这个代码(工作),但在我看来,这不是在'answer'= 1和另一个之间进行分离的最有效方法。

<div id="important-question">
---> Important questions 

	<div ng-repeat="q in questions | filter:{answer:1}" >

		<br/><br/><label> {{q.name}} </label><br/><br/>
	
</div>
</div>

<div id="useless-question">

--->	Useless questions

	<div ng-repeat="q in questions | filter:{answer:0}" >

		<br/><br/><label> {{q.name}} </label><br/><br/>
    
</div>

</div>

如果你看到更好的方法,请告诉我。

谢谢

1 个答案:

答案 0 :(得分:0)

您可以使用groupBy过滤器:

&#13;
&#13;
angular.module('app', ['angular.filter'])
.controller('MyController', ['$scope', function($scope) {
    $scope.questions=[
      {name:'11',answer:1},
      {name:'12',answer:1},
      {name:'01',answer:0},
      {name:'02',answer:0},
      {name:'3',answer:3},
    ]
}]);
&#13;
<script src="//code.angularjs.org/snapshot/angular.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.16/angular-filter.js"></script>

<div ng-app='app' ng-controller="MyController">
    <div ng-attr-id="{{(key == 1 ? 'important' : 'useless') + '-questions'}}" ng-repeat="(key, value) in questions | groupBy: 'answer'" ng-if='key==1||key==0'>
      {{key == 1 ? 'Important' : 'Useless'}} questions
      <div ng-repeat="q in value" >
          <br/><br/><label> {{q.name}} </label><br/><br/> 
      </div>
</div>
&#13;
&#13;
&#13;