2

I'm having a bit of trouble working out why my images aren't rendering properly inside of an ng-repeat with an ng-init, ng-mousover and ng-mouseout.

<div ng-repeat="item in product.items">
    <div ng-init="imgsrc='{{item.image01}}'" 
         ng-mouseover="imgsrc='{{item.image02}}'" 
         ng-mouseout="imgsrc='{{item.image01}}'">
        <img ng-src="{{imgsrc}}" />
    </div>
</div>

The correct paths are rendering inside of ng-init, ng-mouseover and ng-mouseout, but the <img> tag is only updating with {{item.image01}} and {{item.image02}} instead of the actual image paths.

What am I missing here?

10
  • 1
    ng-mouseover="imgsrc=item.image02" Commented Jan 6, 2017 at 19:00
  • Check my answer. Or if you want to stick with your solution try the comment from Alon or remove the single quotes. Commented Jan 6, 2017 at 19:04
  • 1
    After finding the problem, really think about switching this task to CSS. This is such an overkill Commented Jan 6, 2017 at 19:05
  • @AlonEitan - that was it. Commented Jan 6, 2017 at 19:06
  • @azerafati - the idea behind this method is to make sure there is only one image loaded at a time. I'm not aware of a CSS method that can make this work. I'm intrigued, though. Can you explain? Commented Jan 6, 2017 at 19:07

2 Answers 2

3

remove the braces, for assigning values to variables you don't need them inside ng-init.

<div ng-repeat="item in product.items">
    <div ng-init="imgsrc=item.image01" 
         ng-mouseover="imgsrc=item.image02" 
         ng-mouseout="imgsrc=item.image01">
        <img ng-src="{{imgsrc}}" />
    </div>
</div>
Sign up to request clarification or add additional context in comments.

Comments

1

It's fine to assign a value with ng-init, but ng-moseouver and ng-mouseout don't work the same.

Try to create a function and pass it to them:

ng-mouseover="handleMouseover()" 
ng-mouseout="handleMouseout()"

Then update the value of your variable inside of the methods accordingly.

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.