Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between double and single curly brace in angular JS?

Tags:

angularjs

People also ask

What are double curly braces in angular?

Interpolation in Angular In simple terms, when you send data from an Angular component to the template using the mustache syntax or what you would call double curly brackets (these: “{{ … }}”) it is called interpolation. You can use it with variables, objects and even functions.

What is double curly brackets in JavaScript?

The double curly brackets are not HTML but scripting code. The term inside, interest, is a placeholder, sort of like the name and address in a form letter. The string {{interest}} will be replaced when the HTML template is converted into straight HTML that is sent over the network to the user.

Why do curly braces have double reactions?

From documentation The exterior set of curly braces are letting JSX know you want a JS expression. The interior set of curly braces represent a JavaScript object, meaning you're passing in a object to the style attribute.

What does curly braces mean in JavaScript?

Curly braces { } are special syntax in JSX. It is used to evaluate a JavaScript expression during compilation. A JavaScript expression can be a variable, function, an object, or any code that resolves into a value.


{{}} - double curly braces:

{{}} are Angular expressions and come quite handy when you wish to write stuff to HTML:

<div>
    {{planet.name == "Earth" ? "Yeah! We 're home!" : "Eh! Where 're we?"}}
</div>

<!-- with some directives like `ngSrc` -->
<img ng-src="http://www.example.com/gallery/{{hash}}"/>

<!-- set the title attribute -->
<div ng-attr-title="{{celebrity.name}}">...

<!-- set a custom attribute for your custom directive -->
<div custom-directive custom-attr="{{pizza.size}}"></div>

Don't use these at a place that is already an expression!

For instance, the directive ngClick treats anything written in between the quotes as an expression:

<!-- so dont do this! -->
<!-- <button ng-click="activate({{item}})">... -->  

{} - single curly braces:

{} as we know stand for objects in JavaScript. Here, too, nothing different:

<div ng-init="distanceWalked = {mon:2, tue:2.5, wed:0.8, thu:3, fri:1.5, 
sat:2, sun:3}">

With some directives like ngClass or ngStyle that accept map:

<span ng-style="{'color' : 'red'}">{{viruses.length}} viruses found!</span>

<div ng-class="{'green' : vegetable == 'lettuce', 
'red' : vegetable == 'tomato'}">..

no curly braces:

As already mentioned just go braceless when inside expressions. Quite simple:

<div ng-if="zoo.enclosure.inmatesCount == 0">
    Alarm! All the monkeys have escaped!
</div>

 


one more thing about {{}} it is also used as Watcher.. please avoid as much as possible for better performance


I know this is an old post and might be a bit off topic, but this is in response to @DonD and @GafrieldKlon...

It would seem a watcher is actually placed if you were to use the ng-bind directive, not when using {{}}. That being said, I believe @riyas answer above is still partially true in that avoiding {{}} is generally better for performance, but not for the reason stated.

This answer to another post explains this in more detail.