1

I have a scope of 's with their background images, but I can't figure out how to make them change. I tried several variants, but none of them has passed. Here's the code:

<div ng-controller="mainController" class="main">
    <div ng-repeat="land in lands" class="col-md-9" ng-style="{ 'background-image': 'url({{ land.cover }})'}">
        <p>{{ land.name }}</p>
    </div>
</div>

Javascript:

app.controller('mainController', ['$scope', function($scope) {
    $scope.lands = [
        {
            cover: 'img/norway.jpg',
            cover1: 'img/norway1.jpg',
            name: 'Norway'
        },
        {
            cover: 'img/sweden.jpg',
            cover1: 'img/sweden1.jpg',
            name: 'Sweden'
        },
        {
            cover: 'img/denmark.jpg',
            name: 'Danmark'
        },
        {
            cover: 'img/iceland.jpg',
            name: 'Iceland',
        },
        {
            cover: 'img/finland.jpg',
            name: 'Finland'
        },
        {
            cover: 'img/estonia.jpg',
            name: 'Estonia'
        }
    ];
}]);
4
  • literally I want {{ land.cover }} change on hover to {{ land.cover1 }} Commented Jan 27, 2016 at 11:19
  • is it not working for you? maybe add a plnkr to your question Commented Jan 27, 2016 at 11:23
  • plnkr.co/edit/KwcvgrzfHhkrWn17e4b2?p=preview here is it Commented Jan 27, 2016 at 11:26
  • where u have written any code to change ur image from cover to cover1? Commented Jan 27, 2016 at 11:29

3 Answers 3

4

You can't use interpolation tags in the expression context. Should be:

<div ng-controller="mainController" class="main">
    <div ng-repeat="land in lands" class="col-md-9" 
         ng-init="coverImage = land.cover" 
         ng-mouseover="coverImage = land.cover"
         ng-mouseout="coverImage = land.cover1"
         ng-style="{ 'background-image': 'url(' + coverImage + ')' }">
        <p>{{ land.name }}</p>
    </div>
</div>
Sign up to request clarification or add additional context in comments.

4 Comments

I've fixed tags but still don't know how to make background image change. I suppose I should use ng-mouseenter and ng-mouseleave, but have no idea how to use them properly in this situation :(
Check my answer, it has all the answers to your situation, ngMouseover and ngMouseout.
upvoted for simplicity - the ng-init approach never occurred to me :)
@dfsq thanks a lot! that really simplified everything!
2

Since you're trying to respond to the DOM, the angular way of doing this would be to create a directive.

Working code is available here. The summary of the solution is this:

Your DOM will change to this:

<div ng-controller="MainController as ctrl">
    <div ng-repeat="land in ctrl.lands" hover-bg-change default-bg="land.cover" hover-bg="land.cover1" class='tile'>
        <p>{{ land.name }}</p>
    </div>
</div>

The directive will look like this:

app.directive('hoverBgChange', ['$parse', function ($parse)
{
    return {
        restrict: 'A',
        link: function (scope, $el, attrs)
        {
            var defaultBg = $parse(attrs.defaultBg)(scope);
            var hoverBg = $parse(attrs.hoverBg)(scope);

            $el.css('background-image', 'url('+defaultBg+')');

            $el.on('mouseover', function ()
            {
                $el.css('background-image', 'url('+hoverBg+')');
            })
            .on('mouseout', function ()
            {
                $el.css('background-image', 'url('+defaultBg+')');
            });
        }
    }
}])

3 Comments

@dfsq's solution is a much simpler solution. If changing bg image is all you need, you'd be better off with that solution :)
yes, but I tried his variant and it didn't work :( but your variant does exactly what I need
I tried that approach, and it works as well. Updated the plunker to show that approach. I recommend you go with that approach - less code is more maintainable code :)
1

You can't make it work in one go. My solution would be define css as

.hover-out{
background-image: url('img/norway.jpg');
}

.hover-in{
background-image: url('img/norway1.jpg');
}

In markup

<div ng-controller="mainController" class="main">
    <div ng-repeat="land in lands" class="col-md-9" ng-class="{'hover-out':hoverOut[$index], 'hover-in':!hoverOut[$index]}" ng-mouseover="hoverOut[$index] = false" ng-mouseleave="hoverOut[$index] = true">
        <p>{{ land.name }}</p>
    </div>
</div>

1 Comment

urpalreloaded unfortunately it doesn't work :( i made it as you've shown, but now the <div>'s have totally dissapeared

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.