Angular with firebase
Today session is about how connect the angular with firebase
PREREQUISITES
1)Node.js - if you are not already installed click Here
2)npm package manager -
3)text editor(vs code) - if you are not already installed click Here
if you are already install the node.js you can silmply open the command prompt and type
node -v then you will see the follow result.
make a component named employee as follows.
make a service for the employee
make a class for the employee
constructor(private service : EmployeeService, private firestore:AngularFirestore) { }
if you want to more details about this project,
you can visit my github profile and angular-firebase-simple-exameple
PREREQUISITES
1)Node.js - if you are not already installed click Here
2)npm package manager -
3)text editor(vs code) - if you are not already installed click Here
if you are already install the node.js you can silmply open the command prompt and type
node -v then you will see the follow result.
- Install angular cli
open your command prompt and type as follow
- create a new angular project
type your command prompt as follow for start the new angular project.
- open the angular project
- add firebase for the project
- make a firebase porject
visit for the firebase and stasrt a project as follow
i already made a project. then i not going to make another project
after after that click the project then you can see as follow interface. select the web option.
copy the marked area. and open your angular project and find the environment.ts and copy it as follows
you can make a variable as this
firebaseCOnfig : { apiKey: "AIzaSyDrXBzelhkbnLiepTDdWT1ct6-4UTr9xSQ", authDomain: "firestorecrud-cdd76.firebaseapp.com", databaseURL: "https://firestorecrud-cdd76.firebaseio.com", projectId: "firestorecrud-cdd76", storageBucket: "firestorecrud-cdd76.appspot.com", messagingSenderId: "715888619999" }
- src/app/app.module.ts (add firebase)
add following imports for the app.module.ts
and add the following modules for the imports section- import { AngularFireModule } from '@angular/fire';
- import { AngularFirestoreModule } from '@angular/fire/firestore';
- import { environment } from 'src/environments/environment';
- AngularFirestoreModule,
- AngularFireModule.initializeApp(environment.firebaseConfig),
make a component named employee as follows.
make a service for the employee
make a class for the employee
- src/app/employee/employee.model.ts
make a class as follow
- export class Employee {
- id: string;
- fullName: string;
- position: string;
- }
- src/app/employee/employee.service.ts
- import { Injectable } from '@angular/core';
- import { Employee } from './employee.model';
- @Injectable({
- providedIn: 'root'
- })
- export class EmployeeService {
- formData :Employee;
- constructor() { }
- }
- src/app/app.module.ts and add EmployeeService for the providers
- import { BrowserModule } from '@angular/platform-browser';
- import { NgModule } from '@angular/core';
- import { AppRoutingModule } from './app-routing.module';
- import { AppComponent } from './app.component';
- //firebase
- import { AngularFireModule } from '@angular/fire';
- import { AngularFirestoreModule } from '@angular/fire/firestore';
- import { environment } from 'src/environments/environment';
- import { EmployeeComponent } from './employee/employee.component';
- import { EmployeeService } from './employee/employee.service';
- import { FormsModule } from '@angular/forms'
- @NgModule({
- declarations: [
- AppComponent,
- EmployeeComponent
- ],
- imports: [
- BrowserModule,
- AppRoutingModule,
- AngularFirestoreModule,
- AngularFireModule.initializeApp(environment.firebaseConfig),
- FormsModule
- ],
- providers: [EmployeeService],
- bootstrap: [AppComponent]
- })
- export class AppModule { }
- src/app/employee/employee.component.ts
add EmployeeService and AngularFirestore into the inside of the constructor of this as follows
constructor(private service : EmployeeService, private firestore:AngularFirestore) { }
and import the follow things
- import { EmployeeService } from './employee.service';
- import { AngularFirestore } from '@angular/fire/firestore';
- src/app/employee/employee.component.html
add follow code for the make the html page
- <h1 class="display">EMP register</h1>
- <form #form="ngForm" autocomplete="off" (submit)="onSubmit(form)">
- <div class="form-group">
- <input name="fullName" #fullName="ngModel" [(ngModel)]="service.formData.fullName" class="form-control">
- </div>
- <div class="form-group">
- <input name="position" #position="ngModel" [(ngModel)]="service.formData.position" class="form-control">
- </div>
- <div class="form-group">
- <button type="submit" class="btn btn-lg btn-block btn-info">submit</button>
- </div>
- </form>
- src/app/employee/employee.component.ts
add the following function for the component file
- onSubmit(form: NgForm){
- let data = form.value;
- this.firestore.collection('comployee').add(data);
- this.resetForm(form);
- }
according to the above function firebase database will make a collection named 'employee' and add the user inputs
finally employee.component.ts should be as follow
- import { Component, OnInit } from '@angular/core';
- import { NgForm } from '@angular/forms';
- import { EmployeeService } from './employee.service';
- import { AngularFirestore } from '@angular/fire/firestore';
- @Component({
- selector: 'app-employee',
- templateUrl: './employee.component.html',
- styleUrls: ['./employee.component.css']
- })
- export class EmployeeComponent implements OnInit {
- constructor(private service : EmployeeService, private firestore:AngularFirestore) { }
- ngOnInit() {
- }
- onSubmit(form: NgForm){
- let data = form.value;
- this.firestore.collection('complyee').add(data);
- }
- }
- app.component.html
copy and paste this
- <div>
</app-employee> - </div>
this use for the invoke the employee.html file
- app.module.ts
should import the Employee component
- declarations: [
- AppComponent,
- EmployeeComponent
- ],
if you want to more details about this project,
you can visit my github profile and angular-firebase-simple-exameple

Comments
Post a Comment