0
基本的に角度2で構築されたIonic2アプリケーションで作業しています。次のobservableに問題があり、近くにいるすべてのユーザーを返します。それを表示し、それをバックして、データや他の時間を繰り返すことでユーザーを倍増させ、1つの結果しか表示せず、別のユーザーがログインしたときにビューを更新しない場合、問題は何かを把握するように見えるが、本当にそれが堅実であることが必要です。角度2観測可能な問題重複した結果を更新しない
Service.ts
@Injectable()
export class LocationTracker {
public nearme = <[IuserGeoLocation]>[];
public firebaseRef: any;
public geoFire: any;
public space: Subject<any> = new BehaviorSubject(0);
constructor(public zone: NgZone, public fire:Fb, public user:User) {
this.fb = fire.instance().database();
this.firebaseRef = this.fb.ref('geofire');
this.geoFire = new GeoFire(this.firebaseRef);
}
UsersNearME() : Observable<any> {
this.nearme.length = 0;
let geoQuery = this.geoFire.query({
center: [this.lat, this.lng],
radius: 4.609 //kilometers
});
geoQuery.on("key_entered", (key, location, distance) => {
const sender = this.fb.ref('/users/'+ key );
sender.on('value', (snapshot) => {
const profile = snapshot.val().profile;
this.nearme.push({
userid: key,
userloc: location,
userdistance: distance.toFixed(2),
email: profile.email,
name: profile.displayName || 'Anonymous',
image: profile.photoURL || ''
});
this.space.next(this.nearme);
});
// console.log("User near you " + key + " found at " + location + " (" + distance + " km away)");
});
return this.space.asObservable();
}
}
Component.ts
public usersNearMe;
constructor(public locationTracker: LocationTracker) {
this.zone.run(() => {
this.usersNearMe = this.locationTracker.UsersNearME();
});
}
component.html
あなたのコンポーネントが本当に正しく設定されていません<ion-content padding>
<!-- <ion-col class="member-item" (click)="viewUser(member.profile.uid)" width-33 *ngFor="let member of members | async; let i = index" [ngClass]="{ hideme : activeUser === member?.profile.uid }"> -->
<ion-grid>
<ion-row class="member-group" >
<ion-col class="member-item" tappable width-33 *ngFor="let m of usersNearMe | async; let i = index">
{{ m?.userid }}
</ion-col>
</ion-row>
</ion-grid>
</ion-content>
私は非同期パイプなしでどのようにこのアプローチ運賃を参照してください@REPTILE – Kravitz
かかわらngforで非同期を使用できるように文句を購読使用。 – chrispy
ええ、それは動作しますが、それはリアルタイムです:)私はリアルタイムで人々があなたの場所に来るように更新したいと思っていますが、私はそれを犠牲にしなければならないと思います。 – Kravitz