Manuel RODRIGUEZ
Manuel RODRIGUEZ

Reputation: 2161

Use of brackets in AngularJS

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

Upvotes: 2

Views: 2981

Answers (1)

Aurora0001
Aurora0001

Reputation: 13547

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">).

Upvotes: 1

Related Questions