0
私は基本的に、Firebaseのリアルタイムデータを変数に格納する共有サービスが必要です。様々なコンポーネントは、データストアから最新のデータを返すカスタムメソッドをサービスで呼び出します。これは、サービスからFirebaseObservableListを返すときのデータを取得する必要がなく、インスタントデータを意味します。あなたは共有データサービスAngularFire2
を理解してほしい今コンポーネント
import { Component, OnInit } from '@angular/core';
import { trigger, state, transition, style, animate } from '@angular/animations';
// Services
import { TransactionService } from '../../../services/transaction.service';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss'],
animations: [
trigger('fadeInOut', [
transition('void => *', [
style({ opacity: 0 }),
animate(150, style({ opacity: 1 }))
]),
transition('* => void', [
animate(150, style({ opacity: 0 }))
])
])
],
})
export class HomeComponent implements OnInit {
public bought: Array<any>;
public activeTab: string = 'recieved';
constructor(private transactions: TransactionService) {
this.bought = this.transactions.getBought();
}
ngOnInit() {
}
}
PSサービス
import { Injectable } from '@angular/core';
import { AngularFireDatabase, FirebaseObjectObservable, FirebaseListObservable } from 'angularfire2/database';
import { Observable } from 'rxjs/Observable';
import { AuthService } from './auth.service';
@Injectable()
export class TransactionService {
protected userTransactions;
private bought: Array<any> = [];
constructor(private db: AngularFireDatabase, private auth: AuthService) {
// Listener to User Transactions
this.getUserTransactions(this.auth.getUserKey())
.subscribe((transactions) => {
this.userTransactions = transactions;
this.processTransactions();
});
}
getUserTransactions(userKey: string): FirebaseListObservable<any[]> {
return this.db.list('/UserTransactions/' + userKey);
}
getCurrentUserTransactions() {
return this.userTransactions;
}
/**
* Process the Transactions and
* Categorize them
*/
processTransactions() {
this.rewarded = [];
this.userTransactions.forEach(transaction => {
let rewardType = transaction.transactiontype;
switch (rewardType) {
case "bought":
this.bought.push(transaction);
break;
default:
break;
}
});
}
getBought() {
return this.bought;
}
}
を見てください:Guysがようやく見つかったので、私はAngular2と観測