typescript - 如何使用 angularfire2 检查 Firebase 中的值?

标签 typescript angular firebase firebase-realtime-database angularfire2

我使用 Firebase 作为数据库,并通过 AngularFire2 访问它。
这是整个 TodoComponent.ts 文件,因此您可以了解完整情况,但我需要的主要内容是在 finishedTodo() 中。

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})
    }
}

在 finishTodo() 中,我可以设置 finished = true,但在此之前,我想检查 Firebase 字段 finished = true 或 false,然后进行设置切换工作,以便我可以根据这种情况添加特殊的 CSS。

Firebase 如下所示:
enter image description here

如何使用 angularfire2 检查 Firebase 中的 finished=true 值?


更新 - @Aaron Saunders 解决方案适应我的文件

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');
        }
}

最佳答案

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

来自documentation - Retrieving data as objects

关于typescript - 如何使用 angularfire2 检查 Firebase 中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38285988/

相关文章:

javascript - 可观察的http服务

web - polymer 1.2 防火警告 : Exception was thrown by user callback

javascript - 不允许 <> 特殊字符的正则表达式

node.js - 在使用 Webpack 构建 Aurelia 应用程序期间找不到命名空间 'Webpack'

html - 输入字段或帮助文本在字段无效时不会变为红色

javascript - Firebase 和 Google map - 读取数据集并创建标记。如何读取对象?

swift - 类型 'StorageMetadata' 的值没有成员 'downloadURL'

typescript - Typescript能够执行简单的功能组合吗?

reactjs - 如何设置react-router-dom的类型?

angular - 从函数返回 vector 作为自动