-1

I'm completely new to Angular and TypeScript and doing a tutorial (tour of heroes).

There the following line occurs:

const url = `${this.heroesUrl}/${id}`;

It makes a string (?) from

private heroesUrl = 'api/heroes';

and the id:number which is passed as an argument to the function.

I get what happens here, but I don't get the syntax. Why do I use the $ and why the brackets?

I would have expected something like this:

const url = heroesUrl + id.toString();
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
SAM
  • 742
  • 10
  • 24
  • Or if you’re asking why that specific syntax as opposed to some other escape characters, that’s not really a question for SO. – jonrsharpe Oct 04 '17 at 07:01

1 Answers1

1

Template strings in JavaScript are denoted with ticks, rather than quotes.

For example:

const variables = 'EXAMPLE';

let normal = 'A normal string allows no ${variables} ' +
    'and cannot cross lines, without the concatenation trick shown here.';

let template = `A template string does allow ${variables} 
and line breaks too.`;

In the template literal, the token ${variables} will be replaced with the value EXAMPLE - and the line break is also allowed.

Fenton
  • 241,084
  • 71
  • 387
  • 401