4

I sometimes see this kind of syntax inside backticks in TypeScript component files in Angular projects:

<div ${selector} [inDemo]="false" [config]="demoConfig">Demo Content</div>

Can somebody explain me this specific attribute ${}?

How does it work and when should I use it?

ivanasetiawan
  • 879
  • 1
  • 10
  • 26

2 Answers2

3

It's called template literals, It is a feature of ECMASCRIPT6 (ECMASCRIPT2015)

Without using it, you can concat a string with some variables by:

var a = 10, b = 15;
var string = "a equals to " + a + " and b equals to " + b;

By using template litteral, it will be simpler:

var a = 10, b = 15;
var string = `a equals to ${a} and b equals to ${b}`;
Faly
  • 13,291
  • 2
  • 19
  • 37
0

It is template literals: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals;

The are serve for writing multi-line strings:

`string text`

`string text line 1  
string text line 2`

`string text ${expression} string text`

tag`string text ${expression} string text`
Salim Ibrohimi
  • 1,351
  • 3
  • 17
  • 35