-
-
Save cthecheese/db83ce209c03d09cd50f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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