gpt4 book ai didi

javascript - 将 ng-repeat 拆分为两个单独的 div 的最有效方法

转载 作者:行者123 更新时间:2023-11-28 04:31:48 27 4
gpt4 key购买 nike

我有这段代码(有效),但在我看来,这不是区分 '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>

如果您发现更好的方法,请告诉我。

谢谢

最佳答案

您可以使用groupBy过滤器:

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},
]
}]);
<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>

关于javascript - 将 ng-repeat 拆分为两个单独的 div 的最有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44563024/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com