Best way to include specific css/js files with ui-router

末鹿安然 提交于 2020-02-12 21:24:09

问题


I'm looking for the best way to manage the includes of css files,

I've found a great answer : https://stackoverflow.com/a/20404559/3163545 But this is working only with rootProvider.

So how to do something like this with stateProvider :

app.config(['$routeProvider', function($routeProvider){
    $routeProvider
        .when('/some/route/1', {
            templateUrl: 'partials/partial1.html', 
            controller: 'Partial1Ctrl',
            css: 'css/partial1.css'
        })
        .when('/some/route/2', {
            templateUrl: 'partials/partial2.html',
            controller: 'Partial2Ctrl'
        })
        .when('/some/route/3', {
            templateUrl: 'partials/partial3.html',
            controller: 'Partial3Ctrl',
            css: ['css/partial3_1.css','css/partial3_2.css']
        })
}]);

Thank you very much :)


回答1:


This module might help you: https://github.com/manuelmazzuola/angular-ui-router-styles

  1. Install it with Bower via bower install angular-ui-router-styles --save

  2. Ensure that your application module specifies uiRouterStyles as a dependency: angular.module('myApplication', ['uiRouterStyles'])

  3. Add css file(s) relative path to the state data object

.state('state1', {
    url: '/state',
    controller: 'StateCtrl',
    templateUrl: 'views/my-template.html',
    data: {
        css: 'styles/some-overrides.css'
    }
})



回答2:


You may also use oclazyload for this purpose. It works with .js and .css files. From it's documentation:

angular.module('MyModule', ['pascalprecht.translate', [
  '/components/TestModule/TestModule.js',
  '/components/bootstrap/css/bootstrap.css',
  '/components/bootstrap/js/bootstrap.js'
]]);

For more details, visit the link below:

oclazyload - di



来源:https://stackoverflow.com/questions/25199047/best-way-to-include-specific-css-js-files-with-ui-router

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!