【问题标题】:How to rename only one string from ng-repeat如何仅从 ng-repeat 重命名一个字符串
【发布时间】:2019-10-01 20:00:14
【问题描述】:

参考下面的代码,我明白了

0: {status: "DRAFT", $$hashKey: "object:282"}

1: {status: "SUBMITTED", $$hashKey: "object:283"}

2: {status: "APPROVED", $$hashKey: "object:284"}

3: {status: "REJECTED", $$hashKey: "object:285"}

4: {status: "RESUBMIT", $$hashKey: "object:286"}

5: {status: "APPROVAL_NR", $$hashKey: "object:287"}
length: 6
__proto__: Array(0)

作为“statusdata”中的响应,我使用 ng-repeat 将这些数据显示为下拉列表。我想重命名视图中的最后一个“approval_nr”。

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    来自角度文档:https://docs.angularjs.org/api/ng/directive/ngRepeat 您可以使用 $last。

    $last   boolean true if the repeated element is last in the iterator.
    

    您也可以查看这个:Different class for the last element in ng-repeat。 我会像下面这样实现它:

    function renameIfLast(name, isLast) {
      if (isLast) {
        return name.toUpperCase(); // do you rename logic here <<
      } else {
        return name;
      }
    }
    
    <h1 ng-repeat="x in records">{{renameIfLast(x.status, $last)}}</h1>
    

    【讨论】:

    • 我不仅希望获得最后一个值。我只想将该列表中的最后一个值重命名为其他名称
    • toUpperCase 是一个函数
    • @charlietfl 我猜想太多 scala 了 :)
    • @Varvarigos Emmanouil .. $last 帮助解决了这个问题。谢谢你
    猜你喜欢
    • 2016-03-08
    • 2020-05-05
    • 2016-02-13
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 2013-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多