2017-07-26 66 views
1

我在離子3應用程序中使用ngx-pipes,因爲我正在從firebase中拉對象。Angular 4 * ngFor,ngx-pipes獲取頂級密鑰的名稱

我找回我的對象​​,並把它變成我可以在頁面上使用一個變量:

getEvents() { 
    this.firebaseDatabase.getEvents.subscribe(data => { 
     console.log("Events ", data); 
     this.events = data; 
    }, error => { 
     console.log("Events ", error); 
    }); 
} 

我日誌中的數據和它回來像這樣:

eventIDHashed_1: 
    endDateTime:"tomorrow" 
    location:"somewhere" 
    roles: 
     GyyapYhHQDOriruHLvGPKaTOiRp2:"admin" 
    startDateTime:"today" 
    title:"Arisss & Nathan" 
    type:"wedding" 
eventIDHashed_2: 
    endDateTime:"tomorrow" 
    location:"somewhere" 
    roles: 
     GyyapYhHQDOriruHLvGPKaTOiRp2:"admin" 
    startDateTime:"today" 
    title:"Jack & Marlana" 
    type:"wedding" 

的方式我在頁面上顯示它是一個臨時解決方案,但除第一行外,一切工作正常。我需要獲得事件名稱(鍵),它說eventIDHashed_1eventIDHashed_2

<div class="event-container"> 
    <div *ngFor="let event of events | values; let i = index"> 
     <span>{{events[i]}}</span> 
     <span>{{event.title}}</span> 
     <span>{{event.location}}</span> 
     <span>{{event.type}}</span> 
     <span *ngFor="let role of event.roles | pairs"> 
      <span>{{role[0]}}, {{role[1]}}</span> 
     </span> 
     <span>{{event.startDateTime}}</span> 
     <span>{{event.endDateTime}}</span> 
    </div> 
</div> 

我所擁有的一切工作,我能夠獲取所有信息,除了實際的事件ID <span>{{events[i] | keys}}</span>這給了我所有的列表它(endDateTime, location, title, startDateTime...)裏面的鑰匙,但我需要得到事件的id eventIDHashed_1eventIDHashed_2

回答

1

與NGX不鏽鋼管打後,我發現我可以使用pairs操作相當簡單。我已經改變了模板從上述這樣:

<div *ngFor="let event of events | pairs"> 
    <span>{{event[0]}}</span> 
    <span>{{event[1].title}}</span> 
    <span>{{event[1].location}}</span> 
    <span>{{event[1].type}}</span> 
    <span *ngFor="let role of event[1].roles | pairs"> 
     <span>{{role[0]}}, {{role[1]}}</span> 
    </span> 
    <span>{{event[1].startDateTime}}</span> 
    <span>{{event[1].endDateTime}}</span> 
</div> 

NGX-對給我的鍵(位置[0])和所述值(位置[1])的event