2

For example:

<img ng-show="showMe" alt="image" class="img-preview" ng-src="{{imgSource}}"
    style="width: 100%; height: 180px;" />

in this case, why need I to wrap imgSource with braces and don't need the same with showMe?

And how should I recognize cases is it required or not? Is there any list of such requirements or strict rule of usage?

anatol
  • 1,680
  • 2
  • 24
  • 47
  • 1
    A trivial answer would be "because `ngShow` doesn't need a templated string but a boolean, and `ngSrc` needs a templated string". The alternative would be for `ngShow` to always require braces, which would again be an inconvenience - not on dev brain like now, but on dev fingers. – Amadan Dec 25 '18 at 04:38

1 Answers1

1

One of the reasons would be that ng-src is a string type, in most of the use cases the domain of the URL won't change or it will be relative or it will be fixed,

So, if we think about what src can be then it can be one of this three ones:

/

https://example.com

/images

Note: this things won't change for all the images, so what we need to provide is the image name.

and ng-show doesn't have much to hold on. it can be either true or false, so that can be easily compiled to model. where in case of ng-src angular needs to know that at what part it needs to compile the model into value. Recommend read

Just code
  • 13,553
  • 10
  • 51
  • 93
  • @anatol `ng-model` is two way binding, you can not concatenate string and modelname, ng-model will be the variable only. I can not think about the scenario where you have to use the `string + model`. that will be handled on `$scope`. This is how they designed it. – Just code Dec 25 '18 at 05:24
  • thanks, good point. as I edited my question, I want to know rules what operates with this cases. could you provide me link to docs where it is declared? – anatol Dec 25 '18 at 05:28
  • @anatol as I provided recommend read in my answer, it is pretty much of what you should check. – Just code Dec 25 '18 at 05:29
  • @anatol at what point you want to understand cases? the predefined directives are quite clear what to you when according to angualrjs site documentation. – Just code Dec 25 '18 at 05:36