homework/ng1/app/search.ctrl.js

49 lines
1.4 KiB
JavaScript
Raw Normal View History

2017-04-30 20:02:47 +03:00
(function () {
'use strict';
angular.module("app").controller("SearchCtrl", Ctrl);
Ctrl.$inject = ['$http', 'modalService']
function Ctrl($http, modalService) {
var vm = this;
vm.contacts = [];
vm.searchString = "";
vm.deleteContact = deleteContact;
vm.deleteSelected = deleteSelected;
vm.nameCodeFilter = nameCodeFilter;
getContacts();
function getContacts() {
2017-06-10 22:25:11 +03:00
$http.get("/api/contacts").then(function(resp){
2017-04-30 20:02:47 +03:00
vm.contacts = resp.data;
2017-06-10 22:25:11 +03:00
}).catch(function(e) {
2017-04-30 20:02:47 +03:00
console.error("Failed to get contacts", e);
})
2017-06-10 22:25:11 +03:00
2017-04-30 20:02:47 +03:00
};
function deleteContact(id) {
2017-06-10 22:25:11 +03:00
modalService.confirm().then(function(resp) {
$http.delete("/api/contacts/"+id).then(function(resp) {
console.log("Deleted contact "+id)
2017-04-30 20:02:47 +03:00
getContacts();
})
})
};
function deleteSelected() {
return;
};
function matchSearchString(string) {
return void 0 !== string && string.toLowerCase().indexOf(vm.searchString.toLowerCase()) >= 0
}
function nameCodeFilter(contact) {
return 0 === vm.searchString.length || (matchSearchString(contact.name) || matchSearchString(contact.phone));
};
};
})();