pap
pap

Reputation: 115

How can I replace es6 template strings inside constants

I have this constant:

.constant('SOMETHING', {
   name: `${name}`,
   subject: `${surname}`,
})

When I am trying to do this for example SOMETHING.name it says that it's undefined.

Any ideas how I can store template strings and then use it?

Thank you very much in advance.

Upvotes: 2

Views: 2045

Answers (1)

Estus Flask
Estus Flask

Reputation: 223154

This isn't a feature that ES6 template literals provide.

es6-template package may be used to interpolate regular JS string with template literal syntax. At this point ES6 template literal syntax has no real benefits over other template engines.

If the context is the framework (I assume that constant stands for AngularJS constant service), it may beneficial to use template facilities that the framework offers, i.e.

app.constant('SOMETHING', {
   name: '{{ name }}',
   ...
});

app.run((SOMETHING, $interpolate) => {
  let somethingName = $interpolate(SOMETHING.name)({ name: 'name' });
  ...
});

Upvotes: 2

Related Questions