Skip to content

Instantly share code, notes, and snippets.

@cthecheese
Created May 1, 2015 05:05
Show Gist options
  • Save cthecheese/db83ce209c03d09cd50f to your computer and use it in GitHub Desktop.
Save cthecheese/db83ce209c03d09cd50f to your computer and use it in GitHub Desktop.
var myApp = angular.module("myApp", []);
myApp.controller("contactsController", function($scope, contactsFactory){
$scope.contacts = updateContact();
$scope.add = function(newContact){
contactsFactory.add(newContact);
updateContact()
clearForm();
}
function clearForm(){
$scope.newContact = angular.copy(contactsFactory.cleanContact());
}
function updateContact(){
$scope.contacts = contactsFactory.getContacts();
}
});
myApp.factory("contactsFactory", function(){
var _contacts = [];
var _cleanContact = {
name: "",
email: ""
};
var service = {};
service.getContacts = function(){
return _contacts;
}
service.add = function(newContact){
_contacts.push(newContact);
}
service.cleanContact = function(){
return _cleanContact;
}
return service;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment