07.- Proyecto con Firebase y Angular [Crear registros]

En este tutorial vamos a hacer los siguientes puntos:

  • Instalar plugin Bootstrap 4 Snipets.
  • Agregar registros en nuestra base de datos a trav茅s de nuestra aplicaci贸n.

Codigo en git: https://github.com/programadornovato/ngFirebase/commit/7c858b5ec093ab069ef1997607364ca84e9bf59a

src/app/app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import {FormsModule} from '@angular/forms';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

import { AngularFireModule } from '@angular/fire';
import { environment } from '../environments/environment';
import { AngularFireAnalyticsModule } from '@angular/fire/analytics';
import { AngularFirestoreModule } from '@angular/fire/firestore';
import { ListaComponent } from './components/lista/lista.component';
import { AgregaComponent } from './components/agrega/agrega.component';
@NgModule({
  declarations: [
    AppComponent,
    ListaComponent,
    AgregaComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    AngularFireModule.initializeApp(environment.firebase),
    AngularFireAnalyticsModule,
    AngularFirestoreModule,
    FormsModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

src/app/components/agrega/agrega.component.html

<form (ngSubmit)="agregar()" >
    <div class="form-group">
        <label for="">Nombre</label>
        <input type="text" class="form-control" name="name" [(ngModel)]="item.name" >
        <button type="submit" class="btn btn-primary">Agregar</button>
    </div>
</form>

src/app/components/agrega/agrega.component.ts

import { Component, OnInit } from '@angular/core';
import { ConService } from 'src/app/services/con.service';

@Component({
  selector: 'app-agrega',
  templateUrl: './agrega.component.html',
  styleUrls: ['./agrega.component.css']
})
export class AgregaComponent implements OnInit {
  item:any={name:''};
  constructor(private ser:ConService) { }

  ngOnInit(): void {
  }
  agregar(){
    this.ser.addItem(this.item);
  }
}

src/app/services/con.service.ts

import { Injectable } from '@angular/core';
import { AngularFirestore, AngularFirestoreCollection } from '@angular/fire/firestore';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
export interface Item { name: string; }
@Injectable({
  providedIn: 'root'
})
export class ConService {
  private itemsCollection: AngularFirestoreCollection<Item>;
  items: Observable<Item[]>;
  constructor(private afs: AngularFirestore) {
    this.itemsCollection = afs.collection<Item>('items');
    this.items = this.itemsCollection.snapshotChanges().pipe(
      map(actions => actions.map(a => {
        const data = a.payload.doc.data() as Item;
        const id = a.payload.doc.id;
        return { id, ...data };
      }))
    );
  }
  retornaItems(){
    return this.items;
  }
  addItem(item: Item) {
    this.itemsCollection.add(item);
  }
}

馃敆Fontawesome: https://fontawesome.com/v4.7.0/
馃敆Puging de angular: https://marketplace.visualstudio.com/items?itemName=johnpapa.Angular2
馃敆 Documentacion de angularfire: https://github.com/angular/angularfire/blob/master/docs/firestore/collections.md

馃敆Puging de angular: https://marketplace.visualstudio.com/items?itemName=johnpapa.Angular2
馃敆 Documentacion de angularfire: https://github.com/angular/angularfire/blob/master/docs/firestore/collections.md
馃帵[Curso] Bootstrap de 0 a 100 馃寛: https://www.youtube.com/playlist?list=PLCTD_CpMeEKSVmsZJIumVvfDviuW-c9AT
馃帵[Curso] Firebase馃敟de 0 a 100: https://www.youtube.com/playlist?list=PLCTD_CpMeEKQqPh6eKk7_Q-ugWyhO6SKT
馃帵[Curso] Angular js 馃敽[De 0 a 100]: https://www.youtube.com/playlist?list=PLCTD_CpMeEKRbxrnw-tAq6tG-NIUNu9sG
馃帵[Curso] SASS de 0 a 100 馃寛: https://www.youtube.com/playlist?list=PLCTD_CpMeEKQXywJB8KMN_-GccZuVf9ag
馃帵[Curso] LESS de 0 a 100 馃寛: https://www.youtube.com/playlist?list=PLCTD_CpMeEKT70itw70uVs0vlFbvbCPQN

馃敆 Pagina de angular: https://cli.angular.io/
馃敆 Pagina de nodejs: https://nodejs.org/en/
馃敆 Pagina de typescrip: https://www.typescriptlang.org/
馃敆 Pagina oficial: https://www.programadornovato.com

馃敆 Facebook: https://facebook.com/ProgramadorNovatoOficial
馃敆 Twitter: https://twitter.com/programadornova
馃敆 Linkedin: https://www.linkedin.com/in/programadornovato/
馃敆 Instagram: https://www.instagram.com/programadornovato/

Deja un comentario

Tu direcci贸n de correo electr贸nico no ser谩 publicada. Los campos obligatorios est谩n marcados con *