I have the following code (it does not work on jsfiddle but it does if you save it as index.html)
<!DOCTYPE html>
<html ng-app="app">
<head>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.5/angular.min.js"></script>
<script>
var photos = [
{"url":"http://archive.vetknowledge.com/files/images/guinea-pig---tan.jpg"},
{"url":"http://www.rfadventures.com/images/Animals/Mammals/guinea%20pig.jpg"}
];
angular.module('app', [])
.directive('ngBackground', function(){
return function(scope, element, attrs){
var url = attrs.ngBackground;
element.css({
'background-image': 'url(' + url + ')',
'background-size' : 'cover'
});
};
})
.controller('PhotosCtrl', function($scope){
$scope.photos = photos;
$scope.flags = {
modalOpen: false,
imgSrc: null
};
$scope.doModal = function(url){
if(url != false) {
$scope.flags.imgSrc = url;
$scope.flags.modalOpen = true;
} else $scope.flags.modalOpen = false;
};
$scope.sourcifier = function(url){
var temp = document.createElement('a');
temp.href = url;
if(temp.hostname.substring(0, 4) == 'www.')
return temp.hostname.substring(4);
else return temp.hostname;
};
});
</script>
</head>
<body>
<div ng-controller="PhotosCtrl">
<div ng-repeat="photo in photos" class="item">
<img ng-src="{{photo.url}}" ng-click="doModal(photo.url)">
<p><a ng-href="{{photo.url}}" target="_blank">{{sourcifier(photo.url)}}</a></p>
</div>
<div ng-show="flags.modalOpen" ng-click="doModal(false)">
<div ng-background="{{flags.imgSrc}}">my background should be of the clicked image</div>
</div>
</div>
</body>
</html>
That basically loads the images form the array "photos" and displays them. What I am trying to achieve is to use a custom created ng-background directive to change the background image of a div to the clicked image src.
It does not work at all! Now, I amsure that flags.imgSrc is correct, since if I use ng-src on an img tag, it does work. It seems more like that the custom directive ng-background does not "refresh" the value of that flag.
Any ideas why and how this could be fixed? Thanks in advance.