Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component, OnInit } from '@angular/core';
  2. import {KontaktService} from '../kontakt.service'; //importamo in uporabimo v konstruktorju
  3. import{Kontakt} from '../kontakt';
  4.  
  5.  
  6. @Component({
  7.   selector: 'app-kontakti',
  8.   templateUrl: './kontakti.component.html',
  9.   styleUrls: ['./kontakti.component.css'],
  10.   providers: [KontaktService] //dodamo
  11. })
  12. export class KontaktiComponent implements OnInit {
  13.  
  14.   kontakti: Kontakt[];
  15.   kontakt: Kontakt;
  16.   ime: string;
  17.   priimek: string;
  18.   telefonska: string;
  19.  
  20.   constructor(private kontaktService: KontaktService) { //dependency injection, tako vstavimo storitev v...
  21.   }
  22.  
  23.   addKontakt(){
  24.   //for(var i = 1; i <= 1000; i++){
  25.     const novKontakt ={
  26.       ime: this.ime,
  27.       priimek: this.priimek,
  28.       telefonska: this.telefonska
  29.     }
  30.  
  31.     this.kontaktService.dodajKontakt(novKontakt).subscribe(kontakt => {
  32.       this.kontakti.push(kontakt);
  33.       this.kontaktService.pridobiKontakte().subscribe(kontakti => this.kontakti = kontakti); //refresh
  34.  
  35.     });
  36.     console.time("timer");
  37. //  }
  38.   console.timeEnd("timer");
  39.   }
  40.  
  41.   //delete kontakt
  42.   deleteKontakt(id:any){
  43.     var kontakti = this.kontakti;
  44.     this.kontaktService.odstraniKontakt(id).subscribe(data=>{
  45.     if(data.n == 1){
  46.         for(var i = 0; i < kontakti.length; i++){
  47.           if(kontakti[i]._id == id){
  48.             kontakti.splice(i,1);
  49.           }
  50.         }
  51.       }
  52.     })
  53.   }
  54.  
  55.   ngOnInit() { //initiated when component is loaded into browser (poklice, ko se nalozi)
  56.     this.kontaktService.pridobiKontakte().subscribe(kontakti => this.kontakti = kontakti);
  57.   }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement