Why do we use double brackets for ng-src in AugularJS?

I am confused by the expression of AngularJS.

From w3schools.com I found out that

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

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

Is ng-src special case when working with AngularJS expressions?

+5
source share
3 answers

Yes, this is a special case for ng-src , since it waits for the template parameter, which is a string with any interpolation inside ( {{}} ), as indicated in docs .

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

It depends on how the directive is specified on its own.

+4
source

You can use both. When you bind certain dynamic values ​​at a time, use this.

 ng-src="{{myVar}}" 

When you bind static values ​​at a time, use this.

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

Hope this helps you understand.

0
source

This has already been asked, citing this question, may be helpful

Difference between double and single curly brace in angular JS?

0
source

Source: https://habr.com/ru/post/1246683/


All Articles