Created
May 28, 2013 04:11
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
}; | |
}]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why you inject $filter while you do not need it?
return !input ? defaultValue : input;