0

I'm trying to add ng-srcset to an image in link function. When i pass a constant string like "./temp/img3.jpg" it work correctly and add ng-srcset to my image.

link :function(scope,element){
    element.attr("ng-srcset","./temp/img3.jpg");
    $compile(element)(scope);
}

but when i try to pass a variable(checked and has a valid value) as attr value,ng-srcset not added to img.

link :function(scope,element){
    //scope.imageSource => ./temp/img3.jpg
    element.attr("ng-srcset",scope.imageSource);
    $compile(element)(scope);
}
6
  • Why do you need to add ng-srcset via a directive? Also, you can't invoke $compile on the same element (i.e. it's infinite recursion) the directive is applied to. Something else you haven't mentioned is likely causing you a problem because it's straight-forward enough to attach ng-srcset via a directive — plnkr.co/edit/tMzfht01oLXaz0U3pLdf?p=preview Commented Aug 29, 2018 at 5:11
  • thank you for replying. you are right. can i use interpolated string as attribute value?(imageSource contain interpolated string) Commented Aug 29, 2018 at 5:38
  • Absolutely, even the documentation on ng-srcset suggests it. Example of it in use: plnkr.co/edit/LGqE9FWZFePoCZIxC3hd?p=preview Commented Aug 29, 2018 at 5:53
  • should i delete this question? Commented Aug 29, 2018 at 6:51
  • Glad I helped. I can add my comments as an answer if you want to accept it, otherwise feel free to remove. Commented Aug 29, 2018 at 7:22

1 Answer 1

1

As mentioned in the comment trail, there's no need to apply the ng-srcset attribute via a directive when you can assign it directly with an interpolated variable value.

The documentation for ng-srcset also contains an example which prescribes the use of interpolation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.