Skip to content

Instantly share code, notes, and snippets.

@litera
Created March 19, 2014 03:25
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save litera/9634958 to your computer and use it in GitHub Desktop.
Save litera/9634958 to your computer and use it in GitHub Desktop.
string.Format for AngularJS
/* AngularJS string.Format filter
*
* This filter provides string variable replacement similar to C# string.Format("{0}", "something");
*
* Usage: {{ "From model: {0}; and a constant: {1};" | format:model.property:"constant":...:... }}
*/
(function (angular) {
angular
.module("ng")
.filter("format", function () {
return function (input) {
var args = arguments;
return input.replace(/\{(\d+)\}/g, function (match, capture) {
return args[1*capture + 1];
});
};
});
})(angular);
@sorskoot
Copy link

sorskoot commented Aug 2, 2016

Instead of using Number() for the parsing to a number you can add a + in front of it. Like return args[+capture + 1];

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