0

I would like to be able to display some overlay when you focus on an img element. The problem is that the elements are populated from the attributes of an angularjs ng-repeat I would like only a specific img to enable a specific text.

The below example does not work, the ng-class variable resolves correctly though.

<div class="myClass" ng-repeat="i in items">
 <span class="class-img-wrapper">
  <img ng-src="{{i.img}}"  class="class-img-view" ng-init="{{i.name}}_focused = false" ng-focus="{{i.name}}_focused = true" ng-blur="{{i.name}}_focused = false"/>
  <span ng-class="{classOverlayShow: {{i.name}}_focused}" class="class-img-overlay">
    {{i.name}}
  </span>
 </span>
</div>
6
  • doesn't work really isn't very informative. We can't see data, or controller, or know what is or isn't happening....show more code and create a demo that replicates problem Commented Nov 20, 2013 at 21:17
  • doesn't work now has a fiddle :) Commented Nov 20, 2013 at 21:44
  • explain what you expect the ng-class to do based on name? Not making sense without explanation. No idea what _focused is about. Right side of ng-class needs to resolve to true/false Commented Nov 20, 2013 at 21:51
  • also need to improve css selector specificity jsfiddle.net/Zrj9R/4 Commented Nov 20, 2013 at 22:00
  • if you look at the fiddle jsfiddle.net/Zrj9R/3 you can see that the ng-class value is resolved to a variable named bob_focused or frank_focused but the ng-init, ng-focus and ng-blur do not Commented Nov 20, 2013 at 22:00

1 Answer 1

1

That does not look like valid syntax to me:

<span ng-class="{classOverlayShow: {{i.name}}_focused}" class="class-img-overlay">

It should read something along the lines of:

 <span ng-class="{classOverlayShow: i.name + '_focused' }" class="class-img-overlay">

Why not add the focussed field to your models in items?

Instead of ng-init="{{i.name}}_focused = false" and then using ng-focus="{{i.name}}_focused = true" ng-blur="{{i.name}}_focused = false" to create dynamic variables on the fly (which I suspect doesn't work because it isn't valid angular syntax), do this instead:

Controller

$scope.items.forEach(function (i) {
    i.focussed = false;
});

$scope.setOverlay = function (item) {
    item.focussed = true;
};

$scope.removeOverlay = function (item) {
    item.focussed = false;
};

Template

<div class="myClass" ng-repeat="i in items">
 <span class="class-img-wrapper">
  <img ng-src="{{i.img}}"  class="class-img-view" ng-focus="setOverlay(i)" ng-blur="removeOverlay(i)"/>
  <span ng-class="{classOverlayShow: i.focussed }" class="class-img-overlay">
    {{i.name}}
  </span>
 </span>
</div>

However, angular documentation suggests that the ngFocus directive works only for input, textarea, select, window and a tags. So for focus events, you may have to create your own directive and bind to onfocus events yourself, or switch to simpler and more ubiquitous ng-click.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks i updated that part, although it is not valid it does work!
I have suggested a more angular-esque solution.
@AlexEdwards Did you notice the update to the answer?
Yes but I ran out of time to look at it, sometime this weekend hopefully, thank you

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.