Skip to content

Latest commit

 

History

History
209 lines (158 loc) · 4.67 KB

File metadata and controls

209 lines (158 loc) · 4.67 KB

1. Installation and Setup

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.

### 0. Prerequisites

npm install -g angular-cli
npm install -g typings 

1. Create a new project

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.

2. Install AngularFire 2 and Firebase

npm install angularfire2 firebase --save

Now that you have a new project setup, install AngularFire 2 and Firebase from npm.

3. Include Firebase SDK typings

In your tsconfig.json file include the following line in your "files" array:

"files": [
  "node_modules/angularfire2/firebase3.d.ts"
]

This is a temporary step until the Firebase typings are published to npm.

4. Include AngularFire 2 and Firebase in the vendor files

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|js.map)',
      'es6-shim/es6-shim.js',
      'reflect-metadata/**/*.+(js|js.map)',
      'rxjs/**/*.+(js|js.map)',
      '@angular/**/*.+(js|js.map)',
      // above are the existing entries
      // below are the AngularFire entries
      'angularfire2/**/*.js',
      'firebase/*.js'      
    ]
  });
};

5. Build

ng build

Run a build and check the /dist/vendor folder for the angularfire2 and firebase folders.

6. System.js

Open /src/system-config.ts. Modify the file like below:

/** Map relative paths to URLs. */
const map: any = {
  'firebase': 'vendor/firebase/firebase.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.

7. Bootstrap

Open /src/main.ts, inject the Firebase providers, and specify your Firebase configuration. This can be found in your project at the Firebase Console:

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,
  // Initialize Firebase app  
  defaultFirebase({
    apiKey: "<your-key>",
    authDomain: "<your-project-authdomain>",
    databaseURL: "<your-database-URL>",
    storageBucket: "<your-storage-bucket>",
  })
]);

8. Inject AngularFire

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

9. Bind to a list

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',
    (...)
  },
]

10. Serve

ng serve

Run 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.

###Next Step: Retrieving data as objects