25. Firebase
- Fast, scalable, real-time database in the Cloud
- Authentication
- Cloud Functions, Cloud Messaging, Cloud Firestore
- Hosting
- Cloud Storage
- Analytics
- Performance Monitoring
- Notifications, TestLab, Crash Reporting
Our Firebase Realtime No SQL Database:
package.json:
{
"name": "ng5-firebase",
"version": "0.0.0",
"license": "MIT",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "^5.0.0",
"@angular/common": "^5.0.0",
"@angular/compiler": "^5.0.0",
"@angular/core": "^5.0.0",
"@angular/forms": "^5.0.0",
"@angular/http": "^5.0.0",
"@angular/platform-browser": "^5.0.0",
"@angular/platform-browser-dynamic": "^5.0.0",
"@angular/platform-server": "^5.0.0",
"@angular/router": "^5.0.0",
"angularfire2": "^5.0.0-rc.3",
"bootstrap": "^4.0.0-alpha.6",
"core-js": "^2.4.1",
"firebase": "^4.6.1",
"rxjs": "^5.5.2",
"zone.js": "^0.8.14"
},
"devDependencies": {
"@angular/cli": "^1.5.0",
"@angular/compiler-cli": "^5.0.0",
"@angular/language-service": "^4.2.4",
"@types/jasmine": "~2.5.53",
"@types/jasminewd2": "~2.0.2",
"@types/node": "~6.0.60",
"codelyzer": "~3.1.1",
"jasmine-core": "~2.6.2",
"jasmine-spec-reporter": "~4.1.0",
"karma": "~1.7.0",
"karma-chrome-launcher": "~2.1.1",
"karma-cli": "~1.0.1",
"karma-coverage-istanbul-reporter": "^1.2.1",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "~5.1.2",
"ts-node": "~3.2.0",
"tslint": "~5.3.2",
"typescript": "2.4.2"
}
}
Environment:
export const environment = {
production: false,
firebase: {
apiKey: 'AIzaSyDeeZmU6Qkd130VJ7RnFd3wLsSkXbFYDos',
authDomain: 'ng5-firebase.firebaseapp.com',
databaseURL: 'https://ng5-firebase.firebaseio.com',
projectId: 'ng5-firebase',
storageBucket: 'ng5-firebase.appspot.com',
messagingSenderId: '242211192873'
}
};
AppModule:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { AngularFireModule } from 'angularfire2';
import { AngularFireDatabaseModule } from 'angularfire2/database';
import { environment } from '../environments/environment';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpClientModule,
AngularFireModule.initializeApp(environment.firebase),
AngularFireDatabaseModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
AppComponent:
import { Component } from '@angular/core';
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
@Component({
selector: 'app-root',
template: `
<h1 class="text-center text-primary">{{ title }}</h1>
<hr>
<div class="row">
<div class="col-3">
<button type="button" (click)="deleteAll()" class="btn btn-info">Remove All</button>
</div>
<div class="col-6 offset-3">
<div class="input-group">
<span class="input-group-btn">
<button (click)="add(newTechnology.value); newTechnology.value = '';"
class="btn btn-primary" type="button">Add</button>
</span>
<input #newTechnology type="text" class="form-control" placeholder="New Technology ...">
</div>
</div>
<div *ngFor="let technology of technologies | async" class=col-3>
<div class="card card-inverse card-danger text-center" style="width: 15rem;">
<div class="card-block">
<h4 class="card-title"><input type="text" class="form-control"
[(ngModel)]="technology.value"></h4>
<p class="card-text"></p>
<button type="button" (click)="update(technology.key, technology.value)"
class="btn btn-default">Update</button>
<button type="button" (click)="delete(technology.key)"
class="btn btn-default">Delete</button>
</div>
</div>
</div>
</div>
`
})
export class AppComponent {
title = 'Welcome to my Angular 5 Firebase App';
technologiesRef: AngularFireList<any>;
technologies: Observable<any[]>;
constructor(db: AngularFireDatabase) {
this.technologiesRef = db.list('/technologies');
this.technologies = this.technologiesRef.snapshotChanges().map(changes => {
return changes.map(change => ({ key: change.payload.key, ...change.payload.val() }));
});
}
add(newTechnology: string): void {
newTechnology = newTechnology.trim();
if (!newTechnology) { return; }
this.technologiesRef.push({value: newTechnology});
}
update(key: string, newValue: string) {
console.log(key, newValue);
this.technologiesRef.update(key, {value: newValue});
}
delete(key: string) {
this.technologiesRef.remove(key);
}
deleteAll() {
this.technologiesRef.remove();
}
}
Let's have a look at the application in our Chromium Browser:
software is like sex. it's better when it's free. https://ng5-firebase.firebaseapp.com/