Created
November 4, 2013 14:25
-
-
Save jakobloekke/7303217 to your computer and use it in GitHub Desktop.
Angularjs sprintf-style filter for interpolating text with values.
This file contains 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
app.filter('sprintf', function() { | |
function parse(str) { | |
var args = [].slice.call(arguments, 1), | |
i = 0; | |
return str.replace(/%s/g, function() { | |
return args[i++]; | |
}); | |
} | |
return function(str) { | |
return parse(str, arguments[1], arguments[2], arguments[3], arguments[4], arguments[5]); | |
}; | |
}); | |
// Usage: | |
$filter('sprintf')( | |
"Hello %s. It's %s to see you!", | |
"World", | |
"very" | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@miukki your adaption only partly works for me (angularjs 1.3). But if I adapt it a little, it works:
Usage:
{{ 'Hello %s' | sprintf : 'World' }}
thx!