角度表达内增量

时间:2013-09-20 08:59:30

标签: javascript angularjs increment

我试图有条件地显示一些div s(低于3 + ng-repeat s),然后递增一个计数器变量。计数器有助于决定div的可见性。

当我执行与ng-show='foo++ < SOME_LIMIT'类似的操作时,我收到语法错误:

Error: Syntax Error: Token 'undefined' not a primary expression at column NaN of the expression [moveItem.type != 'account' && team.rowCount++] starting at [moveItem.type != 'account' && team.rowCount++].

Error: Syntax Error: Token '2' is an unexpected token at column 42 of the expression [team.expandAccounts || team.rowCount++ < 2] starting at [2]

似乎没有要求提供代码,但我仍在给予我的努力。 我尝试过类似的东西:

<div ng-repeat='request in pagedRequests'
  <tr ng-repeat='(fooId, foo) in returnFoos(request)'>
    <td ng-init='foo.rowCount = 0'>
      {{foo.someAttribute}}
      <!--Here is just an icon shown when the rowCount reaches some limit, say 3.
          Uses ng-switch on foo.rowCount. Skipping this as this doesn't seem
          problematic. This toggles rows' collapsing using foo.expandRows.-->
      <div ng-repeat='bar in foo.bars'
           ng-show='foo.rowCount < 3 || foo.expandRows'>
        <span ng-show='bar.type=="multiRowBar"'
              ng-repeat='row in bar.rows'>
          <span ng-show="foo.expandRows || foo.rowCount++ < 3"> <!--SYNTAX ERROR!-->
            <!--Showing the nested objects with more ng-repeats.-->
        </span>
        <span ng-show='bar.type!="multiRowBar" && foo.rowCount++<3'>  <!--SYNTAX ERROR!-->
          <!-- This adds a single row per bar of this type.-->
        </span>
      </div>
    </td>
  </tr>
</div>

我们不能在角度表达式中使用post / pre递增/递减运算符(如++)吗?

2 个答案:

答案 0 :(得分:2)

为什么不使用像:

这样的方法
<button ng-show='increaseFoo() < SOME_LIMIT'>press  me</button>

<强>控制器

$scope.SOME_LIMIT = 10;     
$scope.foo = 8; 

$scope.increaseFoo = function(){
    return $scope.foo++;
};    

Fiddle

如果$scope.foo = 8;ng-show返回true

如果$scope.foo = 9;ng-show返回false

答案 1 :(得分:1)

这个问题有点无知,这种努力会触发循环$digest调用。 (只是为了从这个问题中弄清楚我正在给出我的解决方案。希望它能帮助有人尝试这些东西)我最终确定的解决方案涉及将所有列表扁平化为单个列表并switch显示特定值$index的图标。