gpt4 book ai didi

css - 如何在 AngularJS 中包含 View /部分特定样式?

转载 作者:数据小太阳 更新时间:2023-10-29 09:06:08 29 4
gpt4 key购买 nike

为我的应用程序使用的各种 View 使用单独样式表的正确/可接受的方法是什么?

目前我在顶部的 View /部分 html 中放置了一个链接元素,但有人告诉我这是不好的做法,尽管所有现代浏览器都支持它,但我明白为什么它不受欢迎。

另一种可能性是将单独的样式表放在我的 index.html 的 head 中,但我希望它仅在以性能的名义加载其 View 时才加载样式表。

这是不是一种不好的做法,因为样式只有在从服务器加载 css 后才会生效,从而导致在慢速浏览器中快速闪现未格式化的内容?尽管我正在本地进行测试,但我还没有亲眼目睹这一点。

有没有办法通过传递给 Angular 的 $routeProvider.when 的对象来加载 CSS?

最佳答案

我知道这个问题现在已经很老了,但是在对这个问题的各种解决方案进行了大量研究之后,我想我可能想出了一个更好的解决方案。

UPDATE 1: Since posting this answer, I have added all of this code to a simple service that I have posted to GitHub. The repo is located here. Feel free to check it out for more info.

UPDATE 2: This answer is great if all you need is a lightweight solution for pulling in stylesheets for your routes. If you want a more complete solution for managing on-demand stylesheets throughout your application, you may want to checkout Door3's AngularCSS project. It provides much more fine-grained functionality.

以防将来有人感兴趣,这是我想出的:

<强>1。为 <head> 创建自定义指令元素:

app.directive('head', ['$rootScope','$compile',
function($rootScope, $compile){
return {
restrict: 'E',
link: function(scope, elem){
var html = '<link rel="stylesheet" ng-repeat="(routeCtrl, cssUrl) in routeStyles" ng-href="{{cssUrl}}" />';
elem.append($compile(html)(scope));
scope.routeStyles = {};
$rootScope.$on('$routeChangeStart', function (e, next, current) {
if(current && current.$$route && current.$$route.css){
if(!angular.isArray(current.$$route.css)){
current.$$route.css = [current.$$route.css];
}
angular.forEach(current.$$route.css, function(sheet){
delete scope.routeStyles[sheet];
});
}
if(next && next.$$route && next.$$route.css){
if(!angular.isArray(next.$$route.css)){
next.$$route.css = [next.$$route.css];
}
angular.forEach(next.$$route.css, function(sheet){
scope.routeStyles[sheet] = sheet;
});
}
});
}
};
}
]);

这个指令做了以下事情:

  1. 它编译(使用 $compile )一个 html 字符串,创建一组 <link /> scope.routeStyles 中每个元素的标签对象使用 ng-repeatng-href .
  2. 它附加了 <link /> 的编译集<head> 的元素标签。
  3. 然后它使用 $rootScope收听'$routeChangeStart'事件。对于每个 '$routeChangeStart'事件,它捕获了“当前”$$route对象(用户即将离开的路线)并从 <head> 中删除其部分特定的 css 文件标签。它还捕获了“下一个” $$route对象(用户将要前往的路线)并将其任何部分特定的 css 文件添加到 <head>标签。
  4. ng-repeat部分编译<link />标记根据添加到 scope.routeStyles 中的内容或从中删除的内容处理所有页面特定样式表的添加和删除。对象。

注意: 这需要您的 ng-app属性在 <html> 上元素,不在 <body> 上或 <html> 内的任何内容.

<强>2。使用 $routeProvider 指定哪些样式表属于哪些路由:

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']
})
}]);

此配置添加了自定义 css用于设置每个页面路由的对象的属性。该对象被传递给每个 '$routeChangeStart'事件为 .$$route .所以当听到 '$routeChangeStart'事件,我们可以捕获css我们指定的属性并附加/删除那些 <link />根据需要标记。请注意,指定 css路线上的属性完全是可选的,因为它在 '/some/route/2' 中被省略了例子。如果路线没有 css属性(property),<head>指令将不会对该路线做任何事情。另请注意,您甚至可以在每个路由中使用多个特定于页面的样式表,如 '/some/route/3'上面的示例,其中 css属性是该路由所需样式表的相对路径数组。

<强>3。你完成了这两件事设置了所需的一切,在我看来,它使用尽可能干净的代码来完成。

关于css - 如何在 AngularJS 中包含 View /部分特定样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15193492/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com