Do I really need to have the src and ng-src attributes in the IMG tag? What is the function of each?

4

I saw here that we have the creation of tag <img> with the attributes src and ng-src .

Do you really need to be like this?

Even more than here the two attributes have the same value.

    
asked by anonymous 20.12.2016 / 16:48

1 answer

4

ng-src is used by AngularJS, src is not. src is what you know about HTML, it loads an image with that name, it can be said to be static. The ng-src is interpreted by AngularJS and if the information that is there is correct it can load an image according to the constant identifier there that will be bound at the appropriate time.

Do not forget that the HTML made for AngularJS is a template that should have parts filled at the time of execution with values that make sense.

This example will take the value of variavel and will use the name to be loaded.

<img ng-src="http://www.seudominio.com.br/imagem/{{variavel}}"/>

Thevaluewillprobablybesetincontroller.

Theexamplebelowwillloadanimagecalled{{variavel}},whichisprobablynotwhatyouwant.

<imgsrc="http://www.seudominio.com.br/imagem/{{variavel}}"/>

Now if you do:

<img ng-src="http://www.seudominio.com.br/imagem/variavel"/>

willloadanimagecalledvariavel,afterallitisnotusingescapetoindicatethatthereisanAngularJScodeandnotpureHTML.

Documentation .

    
20.12.2016 / 17:03