首页 > 其他 > 详细

[Angular 2] Get start with Firebase

时间:2016-04-25 06:44:35      阅读:276      评论:0      收藏:0      [点我收藏+]

Create a Firebase Servcie:

import {Injectable} from angular2/core;
import {Http, Response} from angular2/http;
@Injectable()
export class FirebaseService{
    constructor(private _http: Http){

    }

    addOneHistory(keyword: string){
        const body = JSON.stringify({keyword: keyword});
        return this._http.post(https://xxx.com/searchHistory.json, body)
            .map( (res: Response) => {
                return res.json();
            });
    }

    getHistories(){
        return this._http.get(https://xxxx/searchHistory.json)
            .map( (res: Response)=>{
                return res.json();
            })
            .map( (hObj) => {
                return Object.keys(hObj)
                    .map( (key)=>{
                        return hObj[key];
                    });
            })
    }
}

 

Display the list:

import {Component, OnInit, Input} from angular2/core;
import {FirebaseService} from ./FirebaseService;
@Component({
    selector: history,
    template: `<ul><li *ngFor="#item of histories | async">
    {{item?.keyword}}
</li></ul>`
})

export class HistroyComponent implements OnInit {
    
    histories;

    constructor(private _fireBaseService:FirebaseService) {
    }

    ngOnInit() {
        this.histories = this._fireBaseService.getHistories();
    }
}

 

[Angular 2] Get start with Firebase

原文:http://www.cnblogs.com/Answer1215/p/5429219.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!