<!DOCTYPE html>
<html>
<head>
<script src="https://code.angularjs.org/1.5.8/angular.js" data-require="angular.js@1.5.8" data-semver="1.5.8"></script>
<link href="style.css" rel="stylesheet" />
<script src="script.js"></script>
</head>
<body ng-app="App">
<h1>C# string.Format() AngularJS filter</h1>
<div ng-controller="MainController">
{{'{0} my name is {1}'|textfilter:'Hey':Name}}
</div>
</body>
</html>
// Code goes here
var app = angular.module('App', []);
app.controller('MainController', function($scope) {
$scope.Name = "Ryan Southgate";
});
app.filter('textfilter', [function() {
return function (input) {
if (arguments.length > 1) {
// If we have more than one argument (insertion values have been given)
var str = input;
// Loop through the values we have been given to insert
for (var i = 1; i < arguments.length; i++) {
// Compile a new Regular expression looking for {0}, {1} etc in the input string
var reg = new RegExp("\\{" + (i-1) + "\\}");
// Perform the replace with the compiled RegEx and the value
str = str.replace(reg, arguments[i]);
}
return str;
}
return input;
};
}]);