Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpolation in strings made using backtick operator

I'm learning Angular 2 and I came across a few new things. I used ScratchJS extension of Chrome browser to learn TypeScript. While doing that, this is what I did for a backtick (`) string:

let user='user';
let msg=`Welcome ${user}! 
I can write multi-line string.

This is awesome!
`;
console.log(msg);

As you can see, this is how the variable user is used in the string. But when I do the same thing in an Angular 2 project, it's a bit different (and doing things like above will throw an error). For my dummy Angular 2 project, I made a simple component:

import { Component} from '@angular/core';

@Component({
  selector: 'app-user',
  template: `

      Hi,  {{user}}

      I can write multi-line string.

      This is awesome!
  `,
  styles: []
})
export class UserComponent {

  user:string='John Doe';
  constructor() { 
  }
}

This works. But here, I'm using string interpolation using:

{{}}

instead of:

${}

And if I use that, it will throw an error. I know I understood some facts wrong. But can anyone explain what is it?

like image 729
Lonewolf Avatar asked Apr 18 '17 11:04

Lonewolf


People also ask

What is the use of `` in JavaScript?

The JavaScript in operator is used to check if a specified property exists in an object or in its inherited properties (in other words, its prototype chain). The in operator returns true if the specified property exists.

Can I use string interpolation?

Beginning with C# 10, you can use string interpolation to initialize a constant string. All expressions used for placeholders must be constant strings. In other words, every interpolation expression must be a string, and it must be a compile time constant.

Can you do string interpolation in JavaScript?

String interpolation in JavaScript is a process in which an expression is inserted or placed in the string. To insert or embed this expression into the string a template literal is used. By using string interpolation in JavaScript, values like variables and mathematical expressions and calculations can also be added.

What is string interpolation in angular with an example?

String Interpolation in Angular 8 is a one-way data-binding technique that is used to transfer the data from a TypeScript code to an HTML template (view). It uses the template expression in double curly braces to display the data from the component to the view.


2 Answers

{{ foo }} will be handled by the template engine of Angular, binding the foo property defined in your class.

${ bar } will be handled by the Javascript string interpolation, which, while rendering, have no clue of what is the property bar of your object.

This is something closely related to how Angular work, this is not related to typescript or anything else. You can still use ${} if you are not in your Angular project, or not in the template.

For example, something like this should work, since the expression is evaluated before being returned, and is not dependant of the template engine:

public getUsername(): string {
    let username = 'test';
    return `Hi ${username}`;
}
like image 68
LoïcR Avatar answered Oct 06 '22 09:10

LoïcR


${} it is a string interpolation to display a value of string between typescript code. And {{}} is interpolation to display the content in HTML template.

like image 25
Sushma Tiwari Avatar answered Oct 06 '22 10:10

Sushma Tiwari