您好,我正在尝试动态添加 css,但它不起作用,下面是我的代码
angular.module('myApp', [ 'myApp.controllers','myApp.services']).
config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/new', { templateUrl: 'partials/add.html', controller: 'add' ,resolve: {
style : function(){
/* check if already exists first - note ID used on link element*/
/* could also track within scope object*/
alert("DSAF");
angular.element('head').append(' <link rel="stylesheet" href="css/app.css"/>');
}
}}) }]);
最佳答案
如果没有关于您的代码的更多信息,将很难提供帮助,但无论如何,我想到了两件(简单的)事情:
- 确保路径 css/app.css 重定向到一个文件,即没有 404,
angular.element('head')
仅当 jquery 在 angular 之前加载时才有效,否则您将遇到有关 jqLite 的错误。
我终于组装了一个 plunker here根据您的代码提取,一切都按照应有的方式工作。让我知道是否有帮助。
关于css - 如何在 AngularJS 中动态添加 css?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19946738/