Skip to content

Instantly share code, notes, and snippets.

@shershen08
Created August 13, 2016 08:39
Show Gist options
  • Save shershen08/46d2f4a2ab17c94d6ed565c2e7079b53 to your computer and use it in GitHub Desktop.
Save shershen08/46d2f4a2ab17c94d6ed565c2e7079b53 to your computer and use it in GitHub Desktop.
Angular1 custom filters
angular.module('myApp', [])
/**
* concat
* transforms array [a,b,c] to a template string list 'a,b,c'
* */
.filter('concat', function() {
return function(input, property) {
var out = '';
for (var i = 0; i < input.length; i++) {
out += input[i][property] ? input[i][property] : input[i];
if (i < (input.length - 1)) out += ', ';
}
return out;
};
})
/**
* placeholder
* shows a placeholder in the template if the main value is undefined
* */
.filter('placeholder', function() {
return function(input, property) {
return (input == undefined || input == '') ? property : input;
};
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment