【发布时间】:2016-04-20 13:27:34
【问题描述】:
以数组为例
$scope.users = ['John','Peter'];
如何获取约翰的索引?
【问题讨论】:
标签: javascript angularjs
以数组为例
$scope.users = ['John','Peter'];
如何获取约翰的索引?
【问题讨论】:
标签: javascript angularjs
$scope.users.indexOf('John')
这不是特定角度的。这是一个JS数组方法。
【讨论】:
在您的视图 html {{users.indexOf('John')}} 中尝试此操作或在您的控制器中使用 $scope.users.indexOf('John')
【讨论】:
使用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>
【讨论】: