2

Ich benutze Firebase als Datenbank, und greifen Sie über AngularFire2 darauf zu.
Dies ist die ganze TodoComponent.ts Datei, so erhalten Sie das vollständige Bild, aber die Hauptsache, die ich brauche, ist in finishedTodo().Wie überprüft man einen Wert in Firebase mit angularfire2?

import { Component, OnInit} from '@angular/core'; 
import { AngularFire, FirebaseObjectObservable ,FirebaseListObservable } from 'angularfire2'; 

@Component({ 
    template: ` 
<h2>ToDo</h2> 

<input 
     #newTodo 
     [(ngModel)]="todoinput" 
     placeholder="add ToDo" 
     (keyup.enter)="addTodo(newTodo.value)" 
     type="text" 
/> 
{{ todoinput }} 
<br> 
{{ item | async | json }} ///here I am getting the value as a json string, but I don't know how to make it do what i need 

<div *ngFor="let todo of todos | async"> 
    {{todo.todo}} 
    <button (click)="deleteTodo(todo.$key)">Delete</button> 
    <button (click)="finishedTodo(todo.$key)">Finished</button> 
</div> 

    ` 
}) 

export class ToDoComponent implements OnInit{ 
    item: FirebaseObjectObservable<any>; 
    todos: FirebaseListObservable<any>; 

    constructor(private _af: AngularFire) { 
    } 

    ngOnInit(){ 
     this.todos = this._af.database.list('hr/todos'); 
     this.item = this._af.database.object('/hr/todos/1'); 
    } 

    addTodo(newTodo: string){ 
     this.todos.push({ 
      todo: newTodo 
     }) 
    } 

    deleteTodo(key: string){ 
     if(confirm("U sure u want to delete " + key + " ?")){ 
      this.todos.remove(key) 
     } 
    } 

    finishedTodo(key: string, finished: boolean){ 
     this.todos.update(key,{finished: true}) 
    } 
} 

In finishedTodo() Ich bin in der Lage finished = true zu setzen, aber vorher möchte ich finished = true or false, wenn das Feld Firebase überprüfen und Toggle-Arbeit machen, damit ich auf der Grundlage dieser Bedingung spezielle CSS hinzufügen.

Die Firebase sieht wie folgt aus:
enter image description here

Wie kann ich für finished=true Wert in Firebase überprüfen mit angularfire2?


Update - @Aaron Saunders Lösung meiner Datei angepasst

isFinished; 

finishedTodo(key: string, finished: boolean){ 

    var snapshotFinished = this._af.database.object('hr/todos/'+ key,{ preserveSnapshot: true}) 

    snapshotFinished.subscribe(snapshot => {   
     this.isFinished = snapshot.val().finished; 
    }); 

     if (this.isFinished == false || this.isFinished == null){ 
      this.todos.update(key,{finished: true}); 
      console.log('is false'); 
     }  
     else{ 
      this.todos.update(key,{finished: false}); 
      console.log('is true'); 
     } 
} 

Antwort

2
var i = this._af.database.object('/hr/todos/1', { preserveSnapshot: true }) 
    i.subscribe(snapshot => { 
    console.log(snapshot.key) 
    console.log(snapshot.val().finished) 
    }); 

von documentation - Retrieving data as objects

+0

Dank, Ihre Lösung funktioniert gut für console.log(), aber wenn Ich versuche, meine Datei zu implementieren, ich bekomme eine Endlosschleife. Schau Update1 bitte. –

+0

Ah, ich habe herausgefunden, warum. Es war, weil ich die if-Bedingung in das .subscribe() gesetzt hatte. Ich habe es nach draußen verschoben und es funktioniert jetzt gut :) –

+0

Ich habe die gleichen Probleme, aber ich brauche die if-Anweisung zu überprüfen, ob ein Datensatz bereits da ist oder nicht, wie Sie die if-Anweisung aus dem Abonnement verschieben? – Xvegas