I am trying to get an icon to change on mouseover in AngularJS but am experiencing great difficulty. My menus are generated from JSON objects interpreted into HTML by Angular. Each menu item is its own object with its own title, link, icon, and alternative icon.
Currently my HTML is:
<ul ng-controller="MenuCtrl" class="ul">
<li class="profile-li" ng-repeat="item in profileItems" style="white-space:nowrap" width="250px">
<div width="200px">
<a href="{{item.link}}" class="return">
<div ng-init="itemsrc='{{item.icon}}'" ng-mouseover="itemsrc='{{item.alticon}}'" ng-mouseout="itemsrc='{{item.icon}}'" >
<img class="icon2" ng-src="{{itemsrc}}" style="display:inline; float:left;">
</img>
</div>
</a>
<a href="{{item.link}}" class="return">
<span style="display:inline; white-space:nowrap;"><br> {{item.profileItem}}</span>
</a>
</div>
</li>
</ul>
Within a <div> ng-init sets the initial icon to a variable (itemsrc) equivalent to the icon path specified in the JSON object. ng-mouseover and ng-mouseout change this variable to different paths depending on the JSON values. The <img> within the <div> then uses this established variable as its ng-src.
When the page is loaded, the values in the <div> are generated appropriately (they show the correct paths stored in the JSON object), but the <img src> path is still shown in bracketed notation ({{item.icon}} or {{item.alticon}}), rather than an actual path (icon/assessment.png for example) which causes the image to break.
Here is a sample of the JSON object that is being interpreted: `
[
{
"contentItem": "Orders",
"link": "#orders",
"icon": "icon/orders.png",
"alticon": "icon/ordersb.png"
},
{
"contentItem": "Medication Admin.",
"link": "#medAdmin",
"icon": "icon/medicationAdmin.png",
"alticon": "icon/medicationAdminb.png"
},
{
"contentItem": "Lab Results",
"link": "#labs",
"icon": "icon/labs.png",
"alticon": "icon/labsb.png"
},
{
"contentItem": "Vital Signs",
"link": "#vitals",
"icon": "icon/vitals.png",
"alticon": "icon/vitalsb.png"
},
{
"contentItem": "Assessment",
"link": "#assessment",
"icon": "icon/assessment.png",
"alticon": "icon/assessmentb.png"
},
{
"contentItem": "Intake & Output",
"link": "#io",
"icon": "icon/intakeOutput.png",
"alticon": "icon/intakeOutputb.png"
}
]
Here's an example of what the HTML looks like in the browser:
<li class="profile-li ng-scope" ng-repeat="item in profileItems" style="white-space:nowrap" width="250px">
<div width="200px">
<a href="#face" class="return">
<div ng-init="itemsrc='icon/facesheet.png'" ng-mouseover="itemsrc='icon/facesheetb.png'" ng-mouseout="itemsrc='icon/facesheet.png'">
<img class="icon2" ng-src="{{item.icon}}" style="display:inline; float:left;" src="{{item.icon}}">
</div>
</a>
<a href="#face" class="return">
<span style="display:inline; white-space:nowrap;" class="ng-binding"><br> Face Sheet</span>
</a>
</div>
</li>
ng-mouseoverwill evaluate the givin expression. When using quotes in this expression, you are actually passing a string"{{item.altIcon}}"which will not be parsed. Instead, tryng-mouseover="itemsrc=item.alticon"(same for ngMouseout and ngInit)<li class="profile-li ng-scope" ng-repeat="item in profileItems" style="white-space:nowrap" width="250px"> <div width="200px"> <a href="#face" class="return"> <div ng-init="itemsrc=icon/facesheet.png" ng-mouseover="itemsrc=icon/facesheetb.png" ng-mouseout="itemsrc=icon/facesheet.png"> <img class="icon2" style="display:inline; float:left;"> </div> </a> <a href="#face" class="return"> <span style="display:inline; white-space:nowrap;" class="ng-binding"><br>Face Sheet</span> </a> </div> </li>ng-srcorsrcattribute when the page loads (with method).{{item.icon}}or{{itemsrc}}? Also, do you hav any errors in your developper console?item.alticon, etc. Once the curly braces were removed, it worked correctly. Thank you! I get not using the quotations, but don't understand why the curly braces aren't needed. Is it because its within an angular expression?