Advertisement
Guest User

Untitled

a guest
Jan 7th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit } from '@angular/core';
  2. import {AngularFireDatabase} from 'angularfire2/database';
  3. import { OnDestroy } from '@angular/core/src/metadata/lifecycle_hooks';
  4. import { Subscription } from 'rxjs/Subscription';
  5. import { Observable } from 'rxjs/Observable';
  6. import { AngularFireObject } from 'angularfire2/database/interfaces';
  7. @Component({
  8.   selector: 'app-root',
  9.   templateUrl: './app.component.html',
  10.   styleUrls: ['./app.component.css']
  11. })
  12. export class AppComponent {
  13.  
  14.   courses$;
  15.   itemRef:AngularFireObject<any>;
  16.   courses:any[];
  17.   subscription: Subscription;
  18.  
  19.   constructor(private db:AngularFireDatabase){
  20.     this.courses$ = db.list('/courses').valueChanges();
  21.     this.courses$ = this.db.list('courses').snapshotChanges().map(changes => {
  22.       return changes.map(c => ({ key: c.payload.key, ...c.payload.val() }));
  23.   })
  24.   }
  25.  
  26.   addCourse(author, title)
  27.   {
  28.     let course = {
  29.       author:author.value,
  30.       title:title.value,
  31.     }
  32.     this.db.list('courses').push(course);
  33.  
  34.   }
  35. removeCourse(course:string)
  36. {
  37.   console.log(course);
  38.   this.db.list('courses').remove(course)
  39. }
  40. }
  41.  
  42. //template
  43.  
  44. <mat-form-field class="example-full-width">
  45.     <input matInput placeholder="Nazwa" #title/>
  46.   </mat-form-field>
  47.   <br>
  48. <mat-form-field class="example-full-width">
  49.     <input matInput placeholder="Autor" #author/>
  50.   </mat-form-field>
  51. <br>
  52.  
  53. <button mat-button (click)="addCourse(author, title)">Dodaj</button>
  54.  
  55.   <!!*=|>
  56.       {{course.title}} || {{course.author}}
  57.       <button (click)="removeCourse(course.key)">Usuń</button>
  58.       </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement