【问题标题】:How to find the item index in an array in ANGULAR JS [duplicate]如何在ANGULAR JS的数组中找到项目索引[重复]
【发布时间】:2016-04-20 13:27:34
【问题描述】:

以数组为例

$scope.users = ['John','Peter'];

如何获取约翰的索引?

【问题讨论】:

标签: javascript angularjs


【解决方案1】:
$scope.users.indexOf('John')

这不是特定角度的。这是一个JS数组方法。

【讨论】:

    【解决方案2】:

    在您的视图 html {{users.indexOf('John')}} 中尝试此操作或在您的控制器中使用 $scope.users.indexOf('John')

    【讨论】:

      【解决方案3】:

      使用track by $index

      例如<p ng-repeat = "user in $scope.users track by $index"></p>

      那你就可以判断是不是约翰了:

      <p ng-repeat = "user in $scope.users track by $index">
        <span ng-if="$scope.users[$index] === 'John'">Here's John at {{$index}}</span>
      </p>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-01-29
        • 2021-06-21
        • 2021-10-25
        • 1970-01-01
        • 1970-01-01
        • 2016-11-26
        • 1970-01-01
        相关资源
        最近更新 更多