Search code examples
angularjsorganization

Where should layout functions live in an Angular app?


In the absence of proper CSS3 support, I have a javascript function that keeps our layout looking nice.

var handleWindowResize = function() {
    // Code omitted because i don't think it's relevant
};

It needs to be invoked in the following scenarios

Where is the best place to put this sort of code in an AngularJS app?


Solution

  • You don't want to do this in a controller. Controllers should not reference the DOM. Instead, you want to create a directive to do this. Create a directive that describes our behavior, like "maintain-layout" and put your behavior in that directive.