-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
103 lines (87 loc) · 2.5 KB
/
app.js
File metadata and controls
103 lines (87 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
var myModule = angular.module('app', []);
myModule.service('ContactService', function() {
//to create unique contact id
var uid = 2;
//contacts array to hold list of all contacts
var contacts = [
{ id: 0, 'name': 'World', 'email': 'hello@live.com', 'phone': '123-234-3344' },
{ id: 1, 'name': 'Fred', 'email': 'flintstone@gmail.com', 'phone': '934-123-3444' }];
//save method create a new contact if not already exists
//else update the existing object
this.save = function(contact) {
if (contact.id === null) {
//if this is new contact, add it in contacts array
contact.id = uid++;
contacts.push(contact);
} else {
//for existing contact, find this contact using id
//and update it.
for (var i in contacts) {
if (contacts[i].id == contact.id) {
contacts[i] = contact;
}
}
}
};
this.new = function(contact) {
contact.id = uid++;
contacts.push(contact);
};
//simply search contacts list for given id
//and returns the contact object if found
this.get = function(id) {
for (var i in contacts) {
if (contacts[i].id == id) {
return contacts[i];
}
}
};
//iterate through contacts list and delete
//contact if found
this.delete = function(id) {
for (var i in contacts) {
if (contacts[i].id == id) {
contacts.splice(i, 1);
}
}
};
//simply returns the contacts list
this.list = function() {
return contacts;
};
});
myModule.controller('ContactController', function($scope, ContactService) {
$scope.contacts = ContactService.list();
$scope.saveContact = function() {
ContactService.save($scope.newcontact);
$scope.newContact.id = null;
$scope.details = null;
};
$scope.newContact = function() {
ContactService.new($scope.newcontact);
$scope.newContact.id = null;
$scope.details = null;
};
$scope.deleteContact = function() {
ContactService.delete($scope.newcontact.id);
$scope.newContact.id = null;
$scope.details = null;
};
$scope.addContact = function() {
$scope.details = true;
$scope.newContact.id = null;
}
$scope.cancelContact = function () {
$scope.details = null;
$scope.newContact = {};
}
$scope.delete = function(id) {
ContactService.delete(id);
if ($scope.newContact.id == id) $scope.newcontact = {};
$scope.details = null;
};
$scope.edit = function(id) {
$scope.details = true;
$scope.newContact = angular.copy(ContactService.get(id));
};
})