如何为 angularjs 路由中的每个视图指定 css
来源:互联网 发布:佛经诵读软件 编辑:程序博客网 时间:2024/06/07 05:23
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; }); } }); } }; }]);
该指令执行以下操作:
- 它编译(使用
$compile
)一个HTML字符串,<link />
该scope.routeStyles
对象使用ng-repeat
和对象中的每个项目创建一组标签ng-href
。 - 它将编译的
<link />
元素集合附加到<head>
标签。 - 然后用它
$rootScope
来收听'$routeChangeStart'
事件。对于每个'$routeChangeStart'
事件,它捕获“当前”$$route
对象(用户即将离开的路由),并从<head>
标签中删除其部分特定的css文件。它还抓住“下一个”$$route
对象(用户即将去的路由),并将其部分特定的css文件添加到<head>
标签中。 ng-repeat
编译<link />
标签的一部分根据添加到scope.routeStyles
对象或从对象中删除的内容,处理所有添加和删除页面特定样式表的所有内容。
注意: 这要求您的ng-app
属性在<html>
元素上,而不是<body>
内部的内容<html>
。
$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
属性,该<head>
指令将不会对该路由执行任何操作。还要注意,您甚至可以拥有每个路由的多个特定于页面的样式表,如上'/some/route/3'
例所示,其中的css
属性是该路由所需样式表的相对路径数组。
当您移动到新视图时,该脚本会删除以前视图的样式。如果您不希望发生这种情况,只需从指令中删除以下代码:angular.forEach(current.$$route.css, function(sheet){ delete scope.routeStyles[sheet]; });
$stateProvider
服务为应用程序声明部分特定或路由特定的样式。这可以解决这个问题,让你做这样的事情:app.config(['$stateProvider', function($stateProvider){ $stateProvider .state('state1', { url: '/state1', controller: 'State1controller', template: '<div ui-view></div>', data: { css: [ 'styles/custom-state1.css', { name: 'layout', href: 'styles/state1-layout.css' } ] } }) .state('state1.state12', { url: '/:id', controller: 'State12Controller', templateUrl: 'views/my-template.html', data: { css: [ 'styles/custom-state1.state12.css', { name: 'layout', href: 'styles/state1.state12-layout.css' } ] } }) .state('state2', { url: '/state2', controller: 'State2Controller', templateUrl: 'views/another-template.html', data: { css: ['styles/custom-state2.css', 'styles/another.css'] } }) .state('state3', { url: '/state3', controller: 'State3Controller', templateUrl: 'views/another-super-template.html', data: { css: 'styles/custom-state3.css' } }) // more states can be declared here}]);
如何安装:
用Bower通过安装
bower install angular-ui-router-styles --save
确保您的应用程序模块指定
uiRouterStyles
为依赖关系:angular.module('myApplication', ['uiRouterStyles'])
将指令添加
ui-router-styles
到您的身体标签或任何您想要的地方
<html> <head> <body ng-app="myApp" ui-router-styles> </head></html>
- 向状态数据对象添加css文件相对路径
.state('state1', { url: '/state', controller: 'StateCtrl', templateUrl: 'views/my-template.html', data: { css: 'styles/some-overrides.css' }})
注意事项:
- 在路由上指定一个css属性是完全可选的。如果状态没有css属性,则该服务将对该路由不做任何事情。
- 您甚至可以对每个状态拥有多个特定于页面的样式表,其中css属性是相对路径数组或包含名称和href属性的对象数组。
- 如果存在父状态,则继承数据对象。
该指令执行以下操作:
- 它编译(使用
$compile
)一个html字符串,它data.css
使用ng-repeat
和使用state属性中的每个项目创建一组标签ng-href
。 - 它将编译的
<link />
元素集合附加到<head>
标签。 - 然后用它
$rootScope
来收听'$stateChangeSuccess'
事件。对于每个'$stateChangeSuccess'
事件,它清除之前附加的所有css,并将新的css文件添加到<head>
标签中(如果有的话)。
转载地址:
- 如何为angular路由中的每个视图指定CSS
- angular-route-styles
- ocLazyLoad - Load modules on demand (lazy load) in AngularJS
https://github.com/manuelmazzuola/angular-ui-router-styles
- 如何为 angularjs 路由中的每个视图指定 css
- AngularJS使用路由切换视图
- angularjs 多重视图与路由
- AngularJS 路由和多视图
- angularJS中的路由
- Angularjs中的路由Trick
- 如何为SDI程序中多个不同视图路由命令消息
- AngularJS入门教程07:路由与多视图
- AngularJs多重视图和路由的使用
- AngularJs多重视图和路由的使用
- AngularJS入门教程07:路由与多视图
- AngularJs多重视图和路由的使用
- AngularJS 路由和多视图 之 hashchange
- AngularJS 路由和多视图2
- AngularJS 路由和多视图3
- AngularJS:router路由与多重视图详解
- AngularJs:router路由与多重视图详解
- angularjs中的视图和scope
- 【day-17】快排寻找第K小的数,简化版本
- php7 curl文件上传问题
- UVA 10382 Watering Grass (贪心)
- 字符串的排列
- 制作升级包脚本
- 如何为 angularjs 路由中的每个视图指定 css
- Altium designer不显示飞线的三种方法
- 手写数学公式识别器
- CSS布局之-高度自适应
- 剑指offer_递归与循环---矩形覆盖
- 51nod 1484-猜数游戏(区间交+区间并->map)
- Windows上搭建Python开发环境
- elasticsearch 学习
- UVA1347