2017-05-24 161 views
0

我有一个FirebaseListObservable被循环并且列表中的单个对象被传递,我如何获得通过的对象的键值?如何从数组中的单个对象获取密钥?

对象的单个实例被选中时传递给该函数,我需要获取该键,但是如果使用value.key或value.uid,则它是未定义的。

这是不通过火力点阵列循环

<sebm-google-map-marker 
*ngFor="let m of markers | async" 
(markerClick)="test(m)" 
[latitude]="m.lat" 
[longitude]="m.lng" 

>

当这个点击的功能是

test(m){ 
var things=m; 
console.log(m.val());} 

我想获得该项目的重点是通过

this is the data being passed and want to get the key for the object

+0

请分享一些代码示例为他人理解 – abeyaz

+0

@abeyaz你去那里谢谢你告诉我 –

+0

能告诉你填充组件的'marker'属性的代码?我认为一种解决方案可能是在从Firebase后端加载存储在** marker **对象中的项**键**时。 – Picci

回答

0

你需要拿到钥匙,然后通过收集使用按键循环:

首先让你的按键阵列(可能做到这一点的订阅块,因为它看起来像一个可观察到的在你的代码):

markerKeys = Object.keys(markers); 

然后用键循环:

<sebm-google-map-marker 
    *ngFor="let mKey of markerKeys" 
    (markerClick)="test(markers[mKey])" 
    [latitude]="markers[mKey].lat" 
    [longitude]="markers[mKey].lng" 
0

我相信Eeks33是正确的,但由于标记是不是一个集合,而是一个可观察的集合,你canot治疗呢作为收集。

尝试

const addKeys = (collection => Object.keys(collection) 
            .map(key => collection[key] = key)); 
const markersWithKeys = markers.map(addKeys); 

,那么你应该能够在你的模板过markersWithKeys迭代,每一个项目目前也已一个关键属性。

<sebm-google-map-marker 
*ngFor="let m of markersWithKeys | async" 
(markerClick)="test(m)" 
[latitude]="m.lat" 
[longitude]="m.lng" 

在你的函数clickhandler你应该能够访问关键的,像这样:

test(m) { 
    console.log(m.key); 
} 
相关问题