角度表达式内的增量

Increment inside angular expression

本文关键字:表达式      更新时间:2023-09-26

我试图有条件地显示一些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>

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

为什么不要使用这样的方法:

<button ng-show='increaseFoo() < SOME_LIMIT'>press  me</button>

控制器

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

小提琴

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

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

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