How to append a stylesheet to <head> in AngularJS $routeProvider?

后端 未结 4 1912
一生所求
一生所求 2020-12-15 01:59

I want to load a specific CSS file only when a user accesses the contact.html view on my AngularJS app/site. I found this answer which almost made sense to me H

4条回答
  •  醉酒成梦
    2020-12-15 02:45

    I made a service for it.

    Important part of the code :

    var head = angular.element(document.querySelector('head')); // TO make the code IE < 8 compatible, include jQuery in your page and replace "angular.element(document.querySelector('head'))" by "angular.element('head')"
    
    if(head.scope().injectedStylesheets === undefined)
    {
        head.scope().injectedStylesheets = [];
        head.append($compile("")(scope)); // Found here : http://stackoverflow.com/a/11913182/1662766
    }
    
    head.scope().injectedStylesheets.push({href: "/url/to/style.css"});
    

    Full code in Github : https://github.com/Yappli/angular-css-injector)

提交回复
热议问题