2

My question might be stupid but I am having a hard time trying to determine whether use {} or {{}} or nothing in my HTML with AngularJS.

I came accross the following : 1/ng-src = "{{ mysource }}" 2/ ng-class = "{ active:tab===2 }" 3/ ng-repeat = "foo in foos"

I think I understand that 3/ is an instruction not a text replacement as in 1/ but why use simple brackets in 2/?

Thanks a lot in advance for your help!

Burger

1
  • 1
    1. is being used to evaluate expression (one-way binding). you can evaluate Math expressions such as {{2+3}}. 2. Like u mentioned {} is an object literal in Javascript and it's not much different here too. Let's put it this way , in first scenario your trying to evaluate a value and in second your just assigning a string based on condition. Commented Jul 28, 2016 at 15:17

1 Answer 1

1

The curly braces in example 2 are actually being used to create an object literal (see point 2 here) where the keys map to the class name and the values map to the class value. It's exactly the same as the braces used when writing things like this:

var myData = {
    x: 5,
    y: 6
}; 

The double braces in example 1 are used for AngularJS templates where values are inserted into the HTML by Angular.

Example 3 just uses normal HTML attributes (exactly like <div class="test">).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.