homework/ng1/app/edit.ctrl.js

36 lines
967 B
JavaScript

(function () {
'use strict';
angular.module("app").controller("EditCtrl", Ctrl);
Ctrl.$inject = ['$http', '$routeParams', '$location']
function Ctrl($http, $routeParams, $location) {
var vm = this;
vm.contact = {};
vm.submitForm = submitForm;
getContact($routeParams.id)
function getContact(id) {
if(!id) return
$http.get(`/api/contacts/${id}`).then(resp => {
vm.contact = resp.data;
});
};
function submitForm() {
console.log(vm.contact);
if(vm.contact._id) {
$http.put(`/api/contacts/${vm.contact._id}`, vm.contact).then(resp => {
$location.path("/search");
});
} else {
$http.post(`/api/contacts`, vm.contact).then(resp => {
$location.path("/search");
});
}
};
};
})();