Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular difference between ${ value } and {{ value }}

Tags:

angular

I've been reading tutorials on Angular and some people seem to be using following syntax within their templates ${ value } it seems to produce similar value as simply using {{ value }} so I started wondering why add the dollar sign? Is this done for some semantics or does it actually have its functionality?

like image 692
Ilja Avatar asked Jan 20 '16 16:01

Ilja


People also ask

What does !: Means in Angular?

This specifically means that if the value you are binding to the view is null then it should return null else the actual value, so that you dont get any issues while rendering the template.

What is the meaning of value in Angular?

The AngularJS ng-value directive is used to set the value attribute of an input element, or a select element. It is mainly used on <radio> and <option> elements to set the bound values when these elements are selected. It is supported by <input> and <select> elements.

What is #name in Angular?

The #name syntax is a template reference which refers to the html object, more information can be found on in the Angular docs: Angular template guide. The [(ngModel)] is setting two way binding on the elements value and assigning that to a variable.

How to use a variable in Angular html?

You can declare variables in html code by using a template element in Angular 2 or ng-template in Angular 4+. Templates have a context object whose properties can be assigned to variables using let binding syntax. Note that you must specify an outlet for the template, but it can be a reference to itself.


2 Answers

${someVar} is string interpolation from TS and is applied before the template is processed by angular. {{someVar}} is Angular template binding expression.

like image 176
Günter Zöchbauer Avatar answered Oct 02 '22 12:10

Günter Zöchbauer


To complete what Günter said, the ${someVar} corresponds to the string interpolation feature of ES6. This can be used within strings defined between the charater ` (back-tick). This also allows to define string on several lines.

Here is a sample

let someVar = '10';
let someString = `The value of someVar is ${someVar}`;

It's something that can be used outside Angular2 with ES6.

See this link for more details: https://developers.google.com/web/updates/2015/01/ES6-Template-Strings.

Hope it helps you, Thierry

like image 40
Thierry Templier Avatar answered Oct 02 '22 10:10

Thierry Templier