zoukankan      html  css  js  c++  java
  • [Angular 2] Get start with Firebase

    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();
        }
    }
  • 相关阅读:
    预分频之三
    MySQL超时配置
    随机森林深入理解
    决策树算法——ID3
    指数平滑法
    最小二乘法的Java实现
    JS实战
    CSS布局实战
    Win7 Python开发环境搭建
    神经网络正向传播与反向传播公式
  • 原文地址:https://www.cnblogs.com/Answer1215/p/5429219.html
Copyright © 2011-2022 走看看