2017-05-24 159 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); 
} 
相關問題