Search code examples
cssangularjsangular-ui-routerwebpackwebpack-style-loader

How to unload css when changing route using webpack?


With angular-ui-router changing route unloads the current state's css (using angular-css).

However when the css is packed using webpack the state's css does not unload.

Is there a way to fix this?


Solution

  • We found a workaround but we would prefer a real fix.

    We combined useable styles and two undocumented broadcasts in angular-css ($cssAdd and $cssRemove)

    The code looks like this:

      $rootScope.$on('$cssAdd', function (event, stylesheets) {
        angular.forEach( stylesheets, function(stylesheet){
          if (stylesheet.use)
            stylesheet.use();
        });
      });
      $rootScope.$on('$cssRemove', function (event, stylesheets) {
        angular.forEach( stylesheets, function(stylesheet){
          if (stylesheet.unuse)
            stylesheet.unuse();
        });
      });