Add ngClass to element when child input has :focus

2,373 views
Skip to first unread message

Sebastian Sebald

unread,
Mar 13, 2013, 6:43:04 AM3/13/13
to ang...@googlegroups.com
Hi everyone!

I am new to AngularJS, but find the GoogleGroup and API Docs really helpful so far. But there is one thing I can not figure out how to do in a good way:

How do I add an ngClass to an element when an child input is focused by the user. Quick Example:


<div ng-class="{focus: hasFocus()}">
  <input type="text" ng-model="some.model" value="{{some.model}}"/>
</div>

What should happen is, as soon the input has :focus, the parent div should get "focus" as class attribute. Maybe a directive would be better?

I already found some threads with an similar problem (e.g. http://jsfiddle.net/pkozlowski_opensource/WXJ3p/15/), but they all use the model as part of a parameter, I woul like to void this.

Peter Bacon Darwin

unread,
Mar 17, 2013, 8:39:08 AM3/17/13
to ang...@googlegroups.com

You need to create a directive that sets a variable when its element has focus. Then you can use this in your ng-class directive.

... sent from my tablet

--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 
Reply all
Reply to author
Forward
0 new messages