我有一个电子邮件地址和地址的输入表单。
用户可以通过单击输入字段右侧的 +
符号动态添加最多 3 个电子邮件和地址行。
当我 ng-repeat
输入字段时 +
也会为每个输入字段重复。
如何仅针对 $index
的值最大的最后一个输入字段显示 +。
最佳答案
您可以改为使用 ng-repeat
的 $last
特殊属性。它是为最后一项设置的 bool 值属性(item
这里表示 child scope
由 ng-repeat 创建,而不是那个特定的值ng-repeat
中的迭代)。像这样的东西:
... ng-show="$last"
或
... ng-if="$last"
See other special properties of ng-repeat
- $index - number iterator offset of the repeated element (0..length-1)
- $first - boolean true if the repeated element is first in the iterator.
- $middle - boolean true if the repeated element is between the first and last in the iterator.
- $last - boolean true if the repeated element is last in the iterator.
- $even - boolean true if the iterator position $index is even (otherwise false).
- $odd - boolean true if the iterator position $index is odd (otherwise false).
另请注意,如果您打算使用 ng-show
(而不是 ng-if
),您也可以使用 css 选择器伪类来实现它,例如 :last-child , :last-of-type基于上下文和 DOM 结构等。
关于javascript - AngularJS:ng-show 显示 ng-repeat 中 $index 的最高值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31033423/