38 lines
1.0 KiB
JavaScript
38 lines
1.0 KiB
JavaScript
(function () {
|
|
'use strict';
|
|
|
|
angular.module("app").controller("EditCtrl", Ctrl);
|
|
Ctrl.$inject = ['$http', '$routeParams', '$location', 'contactsService']
|
|
|
|
function Ctrl($http, $routeParams, $location, contactsService) {
|
|
var vm = this;
|
|
|
|
vm.contact = {};
|
|
vm.submitForm = submitForm;
|
|
|
|
var contactId = $routeParams.id;
|
|
|
|
getContact(contactId)
|
|
function getContact(id) {
|
|
if(!id) return
|
|
|
|
contactsService.getContact(id).then(function(contact) {
|
|
vm.contact = contact;
|
|
})
|
|
};
|
|
|
|
function submitForm() {
|
|
console.log("submitting contact", vm.contact);
|
|
if(contactId) {
|
|
contactsService.editContact(contactId, vm.contact).then(function(resp){
|
|
$location.path("/search");
|
|
});
|
|
} else {
|
|
contactsService.addContact(vm.contact).then(function(resp){
|
|
$location.path("/search");
|
|
});
|
|
}
|
|
};
|
|
};
|
|
})();
|