homework/ng1/app/search.ctrl.js

48 lines
1.3 KiB
JavaScript

(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() {
$http.get("/api/contacts").then(resp => {
vm.contacts = resp.data;
}).catch(e => {
console.error("Failed to get contacts", e);
})
};
function deleteContact(id) {
modalService.confirm().then(resp => {
$http.delete(`/api/contacts/${id}`).then(resp => {
console.info(`Contact ${id}`)
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));
};
};
})();