Skip to content

Instantly share code, notes, and snippets.

@kirkbushell
Created May 28, 2013 04:11
Show Gist options
  • Save kirkbushell/5660479 to your computer and use it in GitHub Desktop.
Save kirkbushell/5660479 to your computer and use it in GitHub Desktop.
Default value filter for Angular JS applications - allows you to output a variable, and assign it a default value if it has a value of 0, null, or is an empty string. Very similar to PHP's empty() function, but provides a default value instead of a boolean.
/**
* Sets a default value for a given input.
*
* @param mixed input
* @param string defaultValue
* @return string
*/
module.filter( 'default', [ '$filter', function( $filter ) {
return function( input, defaultValue ) {
if ( !input ) return defaultValue;
return input;
};
}]);
@tom10271
Copy link

Why you inject $filter while you do not need it?

return !input ? defaultValue : input;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment