Skip to content

Instantly share code, notes, and snippets.

@adyngom
Created July 27, 2016 09:23
Show Gist options
  • Save adyngom/022da3b8e4c58d551bd1731831a6a673 to your computer and use it in GitHub Desktop.
Save adyngom/022da3b8e4c58d551bd1731831a6a673 to your computer and use it in GitHub Desktop.
Dynamic Search Filtering in Angular JS Example
(function () {
'use strict';
angular
.module('va', [])
.filter('highlight', highlight)
.factory('query', query)
.controller('searchlist', searchList);
highlight.$inject = ['$sce'];
function highlight($sce) {
return function (text, phrase) {
if (phrase) {
text = text.replace(new RegExp('(' + phrase + ')', 'i'),
'<span class="highlighted">$1</span>');
}
return $sce.trustAsHtml(text);
}
}
query.$inject = ['$http'];
function query($http) {
function getCountries () {
return $http({
method: 'GET',
url: '/json/africa.json'
});
}
return {
listAll: getCountries
}
}
searchList.$inject = ["query"];
function searchList(query) {
var vm = this;
vm.countries = [];
query.listAll()
.success(hasList)
.error(noList);
function hasList(payload) {
vm.countries = payload;
}
function noList(error) {
console.log(error);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment