Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

Proposal: update ng-src even when blank #10051

Open
JasonGoemaat opened this issue Nov 13, 2014 · 5 comments
Open

Proposal: update ng-src even when blank #10051

JasonGoemaat opened this issue Nov 13, 2014 · 5 comments

Comments

@JasonGoemaat
Copy link

ngSrc does not update the src attribute if the value is blank:

    attr.$observe(normalized, function(value) {
      if (!value) { // do not update ngSrc or ngSrcSet if blank
        if (attrName === 'href') {
          attr.$set(name, null);
        }
        return;
      }

I noticed this when answering this question on stackoverflow to create a directive to display an alternate image if there is an error loading the bound image url.

This seems inappropriate to me. Imagine a SPA that loads a new product using $http. If the new product has a blank string for the image URL, the page would continue to display the image for the previous product when even an error placeholder for image not found would be more appropriate. Is there some special reason that the src attribute should not be set to blank when the bound url is empty? If not, I think it should be changed to appropriately reflect the intended binding.

@caitp
Copy link
Contributor

caitp commented Nov 13, 2014

I seem to recall that there was a PR or bug filed about this in the past, let me see if I can find it --- I can't recall what the decision was about this.

@gkalpak
Copy link
Member

gkalpak commented Nov 14, 2014

Originally, ngSrc/ngHref would be set to an emty value if that was the result of the interpolation.

Then, it was reported that some browsers make a request to the current page when the see an empty src attribute, so the behaviour was changed in order not to change the value of the attribute if the interpolation resulted in an empty string: b6e4a71

Later, it was decided that, with href, it was preferrable to remove the attribute (instead of leaving the previous value), because the user could interact with the element and get wrong behaviour. So, it was changed to remove the href attribute when ngHrefgets evaluated to an empty string: 469ea33

Basically, I don't see why we shouldn't remove the src attribute in the same fashion.
Furthermore, with href I would prefer to not remove it and set it to an empty string instead (because some browsers will decorate an <a> element as a link only if it has an href attribute (even an empty one)).

So, this is my suggestion:

attr.$observe(normalized, function(value) {
  if (!value && (attrName !== 'href')) {
    attr.$set(name, null);   // remove ngSrc or ngSrcset if blank
    return;
  }
  ...

@shahata
Copy link
Contributor

shahata commented Nov 14, 2014

When I updated ngHref to remove the href attribute I originally did the same to ngSrc, but @caitp was against it since she was worried it might result in some FOUC behavior, so we decided on leaving ngSrc as it was. See the discussion in #6986.

@caitp
Copy link
Contributor

caitp commented Nov 14, 2014

I knew there was something about this! Yeah, I dunno.. it is possible for this to lead to FOUC issues, buuuut maybe it should be done anyways.

I really don't know :(

@gkalpak
Copy link
Member

gkalpak commented Nov 14, 2014

It is definitely possible, but rather improbable. At least I can't think of a sane use-case, where one might set ngSrc to some non-empty value, then set it to empty and quickly to non-empty again.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants