Che*_*niv 10 javascript angularjs angularjs-filter
我有一个带对象的数组.我想找到一个特定对象的索引.这个对象有一个唯一的id属性'值,我可以找到它$filter:
var el = $filter('filter')( tabs, { id: id })[0]; // "el" is my unique element
Run Code Online (Sandbox Code Playgroud)
但我怎么知道它的原始数组中这个元素的索引是什么?是否$filter可以提供我这个信息?
到目前为止,我没有找到Angular解决方案,因为我无法在此页面上获得更多有用的信息.所以我用过Array的indexOf方法:
var el_index = tabs.indexOf( el );
Run Code Online (Sandbox Code Playgroud)
要获取具体的所有元素的索引,id我们采用类似的方式:
$scope.getTabsIndexes = function(id){
var els = $filter('filter')( tabs , { id: id });
var indexes = [];
if(els.length) {
var last_i=0;
while( els.length ){
indexes.push( last_i = tabs.indexOf( els.shift() , last_i ) );
}
}
return indexes;
}
Run Code Online (Sandbox Code Playgroud)
但它太长了,我确信我在这里重新发明轮子......
试试这个选项:
$scope.search = function(selectedItem) {
$filter('filter')($scope.tabs, function(item) {
if(selectedItem == item.id){
$scope.indexes.push( $scope.tabs.indexOf(item) );
return true;
}
return false;
});
}
Run Code Online (Sandbox Code Playgroud)
我觉得它有点简短明了.
看到 Fiddle
| 归档时间: |
|
| 查看次数: |
9021 次 |
| 最近记录: |