Getting started with AngularFire2 is easy with the Angular CLI. Follow the 10 steps below to get started. Don't worry, we're always working to make this shorter.
The setups below use the Angular CLI.
ng new <project-name>
cd <project-name>The Angular CLI's new command will set up the latest Angular build in a new project structure.
npm install angularfire2 firebase@2.4.2 --saveNow that you have a new project setup, install AngularFire 2 and Firebase from npm.
npm install typings -g
typings install --save --ambient firebaseAngularFire 2 is written in Typescript and depends on typings for the Firebase SDK. To get a clean build, install typings and download the Firebase typings.
Open angular-cli-build.js.
Include AngularFire2 and Firebase in the vendorNpmFiles array:
/* global require, module */
var Angular2App = require('angular-cli/lib/broccoli/angular2-app');
module.exports = function(defaults) {
return new Angular2App(defaults, {
vendorNpmFiles: [
'systemjs/dist/system-polyfills.js',
'systemjs/dist/system.src.js',
'zone.js/dist/*.js',
'es6-shim/es6-shim.js',
'reflect-metadata/*.js',
'rxjs/**/*.js',
'@angular/**/*.js',
// above are the existing entries
// below are the AngularFire entries
'angularfire2/**/*.js',
'firebase/lib/*.js'
]
});
};ng buildRun a build and check the /dist/vendor folder for the angularfire2 and firebase folders.
Open /src/system-config.ts. Modify the file like below:
/** Map relative paths to URLs. */
const map: any = {
'firebase': 'vendor/firebase/lib/firebase-web.js',
'angularfire2': 'vendor/angularfire2'
};
/** User packages configuration. */
const packages: any = {
angularfire2: {
defaultExtension: 'js',
main: 'angularfire2.js'
}
};AngularFire 2 and Firebase need to be mapped with System.js for module loading.
Open /src/main.ts, inject the Firebase providers, and specify your default Firebase:
import { bootstrap } from '@angular/platform-browser-dynamic';
import { enableProdMode } from '@angular/core';
import { <MyApp>, environment } from './app/';
import { FIREBASE_PROVIDERS, defaultFirebase } from 'angularfire2';
if (environment.production) {
enableProdMode();
}
bootstrap(<MyApp>, [
FIREBASE_PROVIDERS,
defaultFirebase('https://<your-firebase-app>.firebaseio.com')
]);Open /src/app/<my-app>.component.ts:
import { Component } from '@angular/core';
import { AngularFire, FirebaseListObservable } from 'angularfire2';
@Component({
moduleId: module.id,
selector: '<my-app>-app',
templateUrl: '<my-app>.component.html',
styleUrls: ['<my-app>.component.css']
})
export class <MyApp>Component {
constructor(af: AngularFire) {
}
}In /src/app/<my-app>.component.ts:
import { Component } from '@angular/core';
import { AngularFire, FirebaseListObservable } from 'angularfire2';
@Component({
moduleId: module.id,
selector: '<my-app>-app',
templateUrl: '<my-app>.component.html',
styleUrls: ['<my-app>.component.css']
})
export class <MyApp>Component {
items: FirebaseListObservable<any[]>;
constructor(af: AngularFire) {
this.items = af.database.list('/items');
}
}Open /src/app/<my-app>.component.html:
<ul *ngFor="let item of items | async">
<li class="text">
{{item.$value}}
</li>
</ul>The async pipe unwraps the each item in the people
observable as they arrive. Also the array that is received through the items observable contains objects that have a $value property. A structure like this:
[
{
$value: 'something',
(...)
},
{
$value: 'something else',
(...)
},
]
ng serveRun the serve command and go to localhost:4200 in your browser.
And that's it! If it totally borke, file an issue and let us know.