ui-router: best practice for updating css of states/views?

124 views
Skip to first unread message

Luis De Avila

unread,
Mar 28, 2014, 7:52:46 PM3/28/14
to ang...@googlegroups.com
Hi all,

Anyone have a best practice or a recommendation for updating the css of views as a user navigates though an app.

Example, I have a list of items. User selects an item and clicks an edit button. The item's edit page slides in from the side.

At the moment I am using a service that has the css for each of the view states.... when the list is full width, when the list is collapsed and the edit page takes up 1/3 of the page, etc.

What I don't like about this approach is that I call functions on the service that set the value of a variable that gets references within ng-class on the various pages. I'm sure other have had to solve this kinds of scenario and am looking to some suggestions.

I've thought about using service that listens to ui-router's state change or view load events to trigger a search of the dom and change the CSS that way. Or maybe there is a ui-router feature that can help with this?

Thanks!

Luis

Gordon Bockus

unread,
Mar 30, 2014, 1:19:02 AM3/30/14
to ang...@googlegroups.com
Hi Luis,
On thing I've found refreshing about angular is state specific css class can just go in the template for that state.  If you are reusing templates for various states, but need to apply different css one option would be to create a constant that has state.current.name -> css classes to apply, and then using a function to apply the state.

getCls: function() {
  return STATE_CLS[$state.current.name];
};

If you have a more specific example of what you're trying to do ppl can answer your specific question.  A plunker is always nice.

Gordon

Luis De Avila

unread,
Apr 3, 2014, 1:58:29 PM4/3/14
to ang...@googlegroups.com
Hi Gordon,

Thanks for the suggestion. I'll play around with that idea today.

Here's a plunker of what I currently have hacked together.
I don't think my solution is very elegant or salable, I'm going to have a lot of pages with edit panels.

Thanks again for the feedback.

Luis

Luis De Avila

unread,
Apr 3, 2014, 6:30:48 PM4/3/14
to ang...@googlegroups.com
For posterity...

I ended up enhancing the provider service so that it would listen for '$locationChangeSuccess' broadcasts. and, as Gordon suggested using the url to drive the CSS that is applied. I used $location.url() and not $state.current.name because ui-router does not detect use of the browser back button or when the url changes from a child state back to a parent state.

Reply all
Reply to author
Forward
0 new messages