6

I'm confused about AngularJS expressions.

From w3schools.com, I learned that

  1. AngularJS expressions can be written inside double braces: {{ expression }}.
  2. AngularJS expressions can also be written inside a directive: ng-bind="expression".

But why do we use ng-src={{...}} instead of ng-src="..."?

Is the ng-src a special case when dealing with AngularJS expressions?

Galaxy
  • 853
  • 2
  • 11
  • 28

3 Answers3

6

Yes, it is a special case for ng-src, as it is waiting for a template parameter, which is a string with any kind of interpolation inside ({{}}), as stated in the docs.

<img ng-src="http://www.gravatar.com/avatar/{{hash}}" alt="Description" />

This depends on how the directive is specified by itself.

Kutyel
  • 8,575
  • 3
  • 30
  • 61
0

You can use both. When you are binding certain dynamic values at that time use like this.

ng-src="{{myVar}}"

When you are binding static values at that time use like this.

<img ng-src="string"></img>

Hope this will make you clear.

C.Champagne
  • 5,381
  • 2
  • 23
  • 35
Dilip Oganiya
  • 1,504
  • 12
  • 20
  • For the directives other than ng-src, for example ng-repeat, ng-model, why don't we use double braces for dynamic values as you said? – Galaxy Apr 08 '16 at 08:15
0

This has sort of been asked already, referring to this question might be helpful

Difference between double and single curly brace in angular JS?

Community
  • 1
  • 1
nardeas
  • 633
  • 6
  • 14
  • I read this before, but still can not understand why we use double braces with ng-src. Is this the only directive that we should use double braces with? – Galaxy Apr 08 '16 at 08:10