Angular Service
JavaScript
function ContactsFactory() {
// change this
var contacts = [];
// to this
var service = {
contacts: [], // <-- Rob's suggestion
get: get, // <-- this also works now as well
add: add,
remove: remove
};
return service;
function get() {
// service.contacts = response from http request.
return service.contacts;
}
function add(contacts) {
angular.forEach(contacts, function(contact) {
service.contacts.push(contact);
});
console.log(service.contacts);
}
function remove(index) {
service.contacts.splice(index, 1);
}
}