homework/ng1/app/edit.ctrl.js

36 lines
1004 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(function(resp) {
vm.contact = resp.data;
});
};
function submitForm() {
console.log("submitting contact", vm.contact);
if(vm.contact._id) {
$http.put("/api/contacts/"+vm.contact._id, vm.contact).then(function(resp){
$location.path("/search");
});
} else {
$http.post("/api/contacts", vm.contact).then(function(resp){
$location.path("/search");
});
}
};
};
})();