Here's a simple sprintf implementation I found. It's built as an angular filter but the logic can of course be pulled out.
https://gist.github.com/jakobloekke/7303217
angular.module('filters')
.filter('sprintf', function() {
function parse(str) {
var args = [].slice.call(arguments, 1),
i = 0;
return str.replace(/%d/g, function() {
return args[i++];
});
}
return function() {
return parse(
Array.prototype.slice.call(arguments, 0,1)[0],
Array.prototype.slice.call(arguments, 1)
);
};
});
Usage:
$filter('sprintf')(
"Hello %d. It's %d nice to see you!",
"World",
"very"
);
or
scope.values = ["World", "very"];
<p ng-bind="message | sprintf: values"></p>