角度表达式内的增量
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
的图标。
相关文章:
- java.net和javascript之间正则表达式的差异
- Grunt匹配正则表达式
- 不同浏览器中的空白字符正则表达式行为
- ng init中的表达式无法使用ng repeat
- 正则表达式在字符串中找到base64
- 我的AngularJS表达式没有'不起作用
- 子字符串/正则表达式以获取字符串中保存的 SRC 值
- 调用正则表达式匹配的函数
- 表达式引擎扩展开发-向呈现的条目添加自定义javascript
- 使用正则表达式评估电子邮件地址时出现性能问题
- Javascript 正则表达式 : ^[^/s/]+[a-z]{1,}[0-9]*[-_]*[^/][
- JavaScript正则表达式文本与RegExp对象
- 正则表达式只允许 x 个整数
- 使用正则表达式将输入格式设置为单字符逗号、单字符逗号等
- 改进用于验证付款金额的正则表达式
- 正则表达式与数字中的第二个点匹配
- 键按正则表达式以查找具有负值的小数
- 一个正则表达式,用于从JS中的HTML标记中删除id、样式和类属性
- "锻造;React中的表达式
- Javascript:应为赋值或函数调用,但实际看到的却是表达式