homework/ng1/app/search.ctrl.js

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