angular-ui-router-styles icon indicating copy to clipboard operation
angular-ui-router-styles copied to clipboard

CSS load after JS in Controller

Open jonatasfreitasv opened this issue 10 years ago • 12 comments

Hello ,

have the following problem , I am using ui- router- style to load a css per route , for example:

 $stateProvider
    .state( 'login' , {
        url: '/login'
        templateUrl 'components/login/form.html'
        date : { css : 'components/login/style.css'} ,
        controller: 'LoginCtrl'
    } ) ;

What is happening is that the CSS is loaded after the js within the controller runs , causing problems .

How can I fix this?

jonatasfreitasv avatar Jun 10 '15 21:06 jonatasfreitasv

How is possible if the css is injected by the js ? Try to explain the problem you're trying to solve, maybe I can help you.

manuelmazzuola avatar Jun 11 '15 08:06 manuelmazzuola

For example:

$stateProvider
    .state( 'login' , {
        url: '/login'
        templateUrl 'components/login/form.html'
        date : { css : 'components/login/style.css'} ,
        controller: 'LoginCtrl'
    } ) ;

in form.html i have:

<div class='foo'></div>

in style.css i have:

.foo {height: 200px;}

in controller i have:

alert($('.foo').heigth());

and the alert show 0px for height.

jonatasfreitasv avatar Jun 11 '15 18:06 jonatasfreitasv

Wait, but is data and not date!

manuelmazzuola avatar Jun 11 '15 18:06 manuelmazzuola

Yes, is data, not date, i wrote wrong here.

jonatasfreitasv avatar Jun 11 '15 18:06 jonatasfreitasv

Well, the CSS is injected by the directive as soon as possible when the state changes successfully, then the browser get it and load it. A solution can be to delay your controller. Another solution, maybe the better one, is to fire an event when the css is loaded and then you can catch it in the controller.

manuelmazzuola avatar Jun 11 '15 18:06 manuelmazzuola

Delay is not a good idea because the css load depends on the client connection.

What would be the event that you suggested ?

jonatasfreitasv avatar Jun 11 '15 19:06 jonatasfreitasv

I am having the same problem. There is a flicker because the html gets loaded and after that the css, so the transition between no css and the css loaded can be seen, making a bad visual effect.

Is there any solution to this?

antoescu avatar Jul 07 '15 13:07 antoescu

Also having the same problem. Unfortunately, if there's not a solution to make the css load faster I can't use. It's a shame because its such a cool solution otherwise. In our case, There is a very long moment where the html renders without the extra CSS. In this time, menus that are styled and vertical display horizontally with bullets, etc.

unleashit avatar Jul 27 '15 04:07 unleashit

Can that link be a solution ? http://www.phpied.com/when-is-a-stylesheet-really-loaded/ I've partially implemented it in the branch bug#7 resolving the bug #7

manuelmazzuola avatar Jul 27 '15 05:07 manuelmazzuola

I created a Angular Service which returns a promise. The promise is resolved after the CSS file(s) is loaded. This works in my case.

https://github.com/zhaocnus/css-load-for-angular-ui-router-styles

zhaocnus avatar Sep 01 '15 00:09 zhaocnus

Hi everyone, my apologies that I never responded but a month ago I was in the middle of trying to meet a big deadline. Manuelmazzoula, when I had not been working with your latest updates to the ui router version. Once I updated, the delay improved significantly. It isn't always perfect, but using ng-animate, I was able hide the content area until everything was rendered and now its perfect.

unleashit avatar Sep 01 '15 00:09 unleashit

Is this fixed?

manuelmazzuola avatar Feb 11 '16 15:02 manuelmazzuola