highlighting selected ul li a in angular

439 views
Skip to first unread message

Tim Wheeler

unread,
Sep 29, 2015, 2:24:44 PM9/29/15
to AngularJS
I'm converting a jQuery app to Angular and I keep fight the tendency to do "jQuery things". 

I have an unordered list (multiple actually) that serve as filters to a dataset using ng-repeat.

When I select one of the list items I want to change the background-color of the li element (really change the style).

I can't use css to do this so I'm thinking it has to be some directive. 

Has anyone seen any samples how a person might do this?

Aviv Ben-Yosef

unread,
Sep 29, 2015, 5:41:27 PM9/29/15
to AngularJS
Hey,

What do you mean when you say "I can't use css"? Why can't you? If you save the currently active item somewhere you can use `ng-class` to selectively add a style for the active item. You can also use `ng-style` with an expression, but I dislike it for the same reason I prefer not to use the style attribute at all in templates.

Anyway, of course you can create a directive to do it. Your directive will just need to look for when the item becomes selected and change the CSS class accordingly. Here's an example.
Reply all
Reply to author
Forward
0 new messages