Search code examples
angulartypescriptstring-formattingstring-interpolation

Typescript populate string template from appsettings


The idea is to have a template string in appSettings.json:

"AppSettings": {
    "PretendUri": "http://devEnvSite/blahBah/{0}/get",
     ...

Reason being this Uri is different from environment to environment.

Then I'd like to use the PretendUri property in several controllers having populated the {0} with a meaningful/dynamic value at run time.

In C# I'd normally use String.Format() but I see this is not the case in Typescript where I need to use interpolation.

Any suggestions?


Solution

  • String replace?

    "http://devEnvSite/blahBah/{0}/get".replace(/{.*}/, "myCustomValue")
    

    Or probably within your code, something like:

    appSettings.PretendUri.replace(/{.*}/, "myCustomValue")