如何在控制器中获取数组项的索引位置并在 angularjs 中传递一个值 [英] how to get index position of an array item in controller passing a value in angularjs
本文介绍了如何在控制器中获取数组项的索引位置并在 angularjs 中传递一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含值列表的数组.
I have an array with list of values.
[Object { id="5", country="UAE"}, Object { id="4", country="India"}]
我想根据 id 中存在的值获取数组项的索引.如何在 angularJS 控制器中获取值 id = 4 的数组项的索引位置?
I want to get the index of array item based on the value present in the id. How can I get the index position of an array item with value of id = 4 in angularJS Controller?
推荐答案
angularjs 的方式(使用 $filter)是这样的
The angularjs way (using $filter) would be something like this
app.controller('MainCtrl', ['$scope', '$filter', function($scope, $filter) {
//array
var items = [{ id: "5", country: "UAE" }, { id: "4", country: "India" }];
//search value
var id2Search = "4";
//filter the array
var foundItem = $filter('filter')(items, { id: id2Search }, true)[0];
//get the index
var index = items.indexOf(foundItem );
}]);
这篇关于如何在控制器中获取数组项的索引位置并在 angularjs 中传递一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文