AngularJS Ng重复删除循环中

时间:2017-06-21 10:12:55

标签: javascript angularjs angularjs-ng-repeat

Angular Js Ng在循环中的最后一个单词后重复删除“,” 我已尝试使用$ last但我在数组上使用selected = true条件进行过滤。

<span ng-repeat="primaryPrograms in ctrl.primaryProgramStudies" ng-show="primaryPrograms.selected == true">
    {{primaryPrograms.name}} , 
</span>

输出:ACT , AP , Math(non-SAT) ,

使用索引:

我必须从循环中获取select = true值

<span ng-repeat="primaryPrograms in ctrl.primaryProgramStudies track by $index" ng-show="primaryPrograms.selected == true"> 
        {{$index}}  - {{primaryPrograms.name}} <span ng-if="!$last">, </span>
</span>

1 - ACT,4 - AP,7 - 数学(非SAT),

我想在最后一个Word之后删除。 ?

4 个答案:

答案 0 :(得分:4)

您可以使用ng-if="!$last"删除最后一个逗号(,)

代码:

<span ng-repeat="primaryPrograms in ctrl.primaryProgramStudies track by $index" ng-show="primaryPrograms.selected == true">
    {{primaryPrograms.name}} <span ng-if="!$last">, </span>
</span>

编辑\更新:

您可以使用filter,因为那里有额外的ng-show条件,(过滤后移除ng-show

 <div ng-repeat="primaryPrograms in ctrl.primaryProgramStudies | filter: {selected  : true} track by $index">
    <span>{{primaryPrograms.name}}</span><span ng-if="!$last">,</span>
  </div>

答案 1 :(得分:1)

尝试下面的代码,你可以隐藏&#34;,&#34;如果$ index是最后一个值

<span ng-repeat="primaryPrograms in ctrl.primaryProgramStudies" ng-show="primaryPrograms.selected == true">
{{primaryPrograms.name}} <span ng-hide="$index==ctrl.primaryProgramStudies.length-1">,</span> 

答案 2 :(得分:0)

试试这个

 <span ng-repeat="primaryPrograms in ctrl.primaryProgramStudies" ng-show="primaryPrograms.selected == true">
   {{primaryPrograms.name + (!$last ? ',':'')}} 
</span>

答案 3 :(得分:-1)

尝试

  

ng-if =“$ index!= 0”

<span ng-repeat="primaryPrograms in ctrl.primaryProgramStudies" ng-show="primaryPrograms.selected == true">
    <i ng-if="$index!=0">,</i>{{primaryPrograms.name}} 
</span>