Guest User

Untitled

a guest
Feb 20th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. <form (submit)="addContact()">
  2. <div class="form-group">
  3. <label for="first_name">First Name</label>
  4. <input type="text" [(ngModel)]="first_name" name="first_name" class="form-control">
  5. </div>
  6. <!-- /.form-group -->
  7. <div class="form-group">
  8. <label for="first_name">Last Name</label>
  9. <input type="text" [(ngModel)]="last_name" name="last_name" class="form-control">
  10. </div>
  11. <!-- /.form-group -->
  12. <div class="form-group">
  13. <label for="first_name">Phone</label>
  14. <input type="text" [(ngModel)]="phone" id="phone" name="phone" class="form-control" required>
  15. </div>
  16. <!-- /.form-group -->
  17. <input type="submit" class="btn-btn-success" value="Add Contact">
  18. </form>
  19.  
  20. import { Component, OnInit } from '@angular/core';
  21. import {ContactService} from '../contact.service';
  22. import {Contact} from '../contact';
  23.  
  24.  
  25. @Component({
  26. selector: 'app-contacts',
  27. templateUrl: './contacts.component.html',
  28. styleUrls: ['./contacts.component.css'],
  29. providers:[ContactService]
  30. })
  31. export class ContactsComponent implements OnInit {
  32.  
  33. contacts: Contact[];
  34. contact:Contact;
  35. first_name:string;
  36. last_name:string;
  37. phone: string;
  38.  
  39. constructor(private contactService: ContactService) {}
  40.  
  41. addContact(){
  42.  
  43. const newContact = {
  44. first_name: this.first_name,
  45. last_name : this.last_name,
  46. phone: this.phone
  47. }
  48. this.contactService.addContact(newContact)
  49. .subscribe(contact=>{
  50. console.log(contact); //displaymenssage
  51. this.contacts.push(contact);
  52. this.contactService.getContacts() .subscribe( contacts => this.contacts = contacts);
  53. });
  54.  
  55. }
  56.  
  57. deleteContact(id:any){
  58. var contacts = this.contacts;
  59. this.contactService.deleteContact(id)
  60. .subscribe(data=>{
  61. if(data.n==1) {
  62. for(var i=0; i<contacts.length; i++) {
  63. if(contacts[i]._id == id) {
  64. contacts.splice(i,1);
  65. }
  66. }
  67. }
  68. })
  69. }
  70. ngOnInit() {
  71. this.contactService.getContacts()
  72. .subscribe( contacts =>
  73. this.contacts = contacts);
  74. }
  75.  
  76. }
Add Comment
Please, Sign In to add comment