2

我试图有条件地显示一些divs(低于 3+ ng-repeats),然后增加一个计数器变量。计数器有助于决定 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>

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

4

2 回答 2

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

于 2013-09-20T09:15:38.617 回答
1

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

于 2013-11-12T09:26:35.627 回答