简体   繁体   中英

How to set data-src on img tag when it has src attr when using AngularJs

I am facing a stupid problem. I am trying to make my images lazy load in my angular app, I have tried this 2 :

<img src="/img/blank.gif" data-src="{{appCtrl.img}}" >
<img src="/img/blank.gif" ng-attr-data-src="{{appCtrl.img}}" >

But they both set the src attr instead of data-src , am i missing someting here ? The only way to keep the data-src attr is not to have any src attr ??? This works as expected :

<img ng-attr-data-src="{{appCtrl.img}}" >
<img data-src="{{appCtrl.img}}" >

Why? How to set data-src on img tag when it has src attr?

This might help you-

You can do it using Jquery-

$("img").attr("data-src","appCtrl.img");

JsFiddle-

https://jsfiddle.net/codecore/2fqbbsst/

I am not quite sure how angular plays in here, so i am not full sure how it plays into the problem; anyways data attributes are just loaded into the dataset on load, meaning if you have the data-src attribute a src property will be created in the dataset object for this element. That said, you can use this:

yourDOMElement.dataset.src = "{{appCtrl.img}}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM