homework/ng1/app/edit.ctrl.js

36 lines
1004 B
JavaScript
Raw Normal View History

2017-04-30 20:02:47 +03:00
(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
2017-06-10 22:25:11 +03:00
$http.get("/api/contacts/"+id).then(function(resp) {
2017-04-30 20:02:47 +03:00
vm.contact = resp.data;
});
};
function submitForm() {
2017-06-10 22:25:11 +03:00
console.log("submitting contact", vm.contact);
2017-04-30 20:02:47 +03:00
if(vm.contact._id) {
2017-06-10 22:25:11 +03:00
$http.put("/api/contacts/"+vm.contact._id, vm.contact).then(function(resp){
2017-04-30 20:02:47 +03:00
$location.path("/search");
});
} else {
2017-06-10 22:25:11 +03:00
$http.post("/api/contacts", vm.contact).then(function(resp){
2017-04-30 20:02:47 +03:00
$location.path("/search");
});
}
};
};
})();