like i said you need to set it vial javascript … i think there is no way to add this afterwards --> your DOM is rendered before the download has finished.
Maybe you can use javascript selectors to select all .scroll-content elements and add the background image for those elements.
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
ImgCache.options.debug = false;
ImgCache.options.usePersistentCache = true;
ImgCache.options.chromeQuota = 50*1024*1024;
// Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
// for form inputs)
if (window.cordova && window.cordova.plugins.Keyboard) {
cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
cordova.plugins.Keyboard.disableScroll(true);
}
if (window.StatusBar) {
// org.apache.cordova.statusbar required
StatusBar.styleDefault();
}
ImgCache.init(function() {
console.log('ImgCache init: success!');
}, function(){
console.error('ImgCache init: error! Check the log for errors');
});
});
})
And created a directive:
.directive('cacheBackground', function($timeout) {
return {
restrict: 'A',
link: function(scope, el, attrs) {
// timeout to give time to init imgCache
$timeout(function() {
ImgCache.isBackgroundCached(el, function(path, success) {
if (success) {
ImgCache.useCachedBackground(el);
} else {
ImgCache.cacheBackground(el, function() {
ImgCache.useCachedBackground(el);
});
}
});
}, 200);
}
};
})
Modified DomHelpers.getBackgroundImage in imgCache.js to use getComputedStyle even when we have jQueryLite:
DomHelpers.getBackgroundImage = function (element) {
if (ImgCache.jQuery) {
return element.attr('data-old-background') ? "url(" + element.attr('data-old-background') + ")" : element.css('background-image');
} else if (ImgCache.jQueryLite) {
var style = window.getComputedStyle(element[0], null);
if (!style) {
return;
}
return element[0].getAttribute("data-old-background") ? "url(" + element[0].getAttribute("data-old-background") + ")" : style.backgroundImage;
} else {
var style = window.getComputedStyle(element, null);
if (!style) {
return;
}
return element.getAttribute("data-old-background") ? "url(" + element.getAttribute("data-old-background") + ")" : style.backgroundImage;
}
};
And then I’ve applied the directive to ion-content in my view:
<ion-content cache-background>
</ion-content>
And now the background-image is working offline too.