AngularCSS--关于angularjs动态加载css文件的方法(仅供参考)
AngularCSS
CSS on-demand for AngularJS
Optimize the presentation layer of your single-page apps by dynamically injecting stylesheets as needed.
AngularCSS listens for route (or states) change events, adds the CSS defined on the current route and removes the CSS from the previous route. It also works with directives in the same fashion with the compile and scope destroy events. See the code samples below for more details.
Read the Article
Introducing AngularCSS: CSS On-Demand for AngularJS
Demos
Angular's ngRoute Demo (source)
Quick Start
Install and manage with Bower or jspm. A CDN is also provided by cdnjs.com
$ bower install angular-css
$ jspm install github:castillo-io/angular-css
1) Include the required JavaScript libraries in your index.html
(ngRoute and UI Router are optional).
<script src="/libs/angularjs/1.5.6/angular.min.js"></script>
<script src="/libs/angularjs/1.5.6/angular-routes.min.js"></script>
<script src="/libs/angular-css/angular-css.min.js"></script>
2) Add angularCSS
as a dependency for your app.
var myApp = angular.module('myApp', ['ngRoute','angularCSS']);
NOTE: The module name "door3.css" is now deprecated.
Examples
This module can be used by adding a css property in your routes values, directives or by calling the $css
service methods from controllers and services.
The css property supports a string, an array of strings, object notation or an array of objects.
See examples below for more information.
In Components
myApp.component('myComponent', {
css: 'my-component/my-component.css' // <--- magic!
templateUrl: 'my-component/my-component.html',
});
In Directives
myApp.directive('myDirective', function () {
return {
restrict: 'E',
templateUrl: 'my-directive/my-directive.html',
/* Binding css to directives */
css: 'my-directive/my-directive.css'
}
});
In Controllers
myApp.controller('pageCtrl', function ($scope, $css) { // Binds stylesheet(s) to scope create/destroy events (recommended over add/remove)
$css.bind({
href: 'my-page/my-page.css'
}, $scope); // Simply add stylesheet(s)
$css.add('my-page/my-page.css'); // Simply remove stylesheet(s)
$css.remove(['my-page/my-page.css','my-page/my-page2.css']); // Remove all stylesheets
$css.removeAll(); });
For Routes (Angular's ngRoute)
Requires ngRoute as a dependency
myApp.config(function($routeProvider) { $routeProvider
.when('/page1', {
templateUrl: 'page1/page1.html',
controller: 'page1Ctrl',
/* Now you can bind css to routes */
css: 'page1/page1.css'
})
.when('/page2', {
templateUrl: 'page2/page2.html',
controller: 'page2Ctrl',
/* You can also enable features like bust cache, persist and preload */
css: {
href: 'page2/page2.css',
bustCache: true
}
})
.when('/page3', {
templateUrl: 'page3/page3.html',
controller: 'page3Ctrl',
/* This is how you can include multiple stylesheets */
css: ['page3/page3.css','page3/page3-2.css']
})
.when('/page4', {
templateUrl: 'page4/page4.html',
controller: 'page4Ctrl',
css: [
{
href: 'page4/page4.css',
persist: true
}, {
href: 'page4/page4.mobile.css',
/* Media Query support via window.matchMedia API
* This will only add the stylesheet if the breakpoint matches */
media: 'screen and (max-width : 768px)'
}, {
href: 'page4/page4.print.css',
media: 'print'
}
]
}); });
For States (UI Router)
Requires ui.router as a dependency
myApp.config(function($stateProvider) { $stateProvider
.state('page1', {
url: '/page1',
templateUrl: 'page1/page1.html',
css: 'page1/page1.css'
})
.state('page2', {
url: '/page2',
templateUrl: 'page2/page2.html',
css: {
href: 'page2/page2.css',
preload: true,
persist: true
}
})
.state('page3', {
url: '/page3',
templateUrl: 'page3/page3.html',
css: ['page3/page3.css','page3/page3-2.css'],
views: {
'state1': {
templateUrl: 'page3/states/page3-state1.html',
css: 'page3/states/page3-state1.css'
},
'state2': {
templateUrl: 'page3/states/page3-state2.html',
css: ['page3/states/page3-state2.css']
},
'state3': {
templateUrl: 'page3/states/page3-state3.html',
css: {
href: 'page3/states/page3-state3.css'
}
}
}
})
.state('page4', {
url: '/page4',
templateUrl: 'page4/page4.html',
views: {
'state1': {
templateUrl: 'states/page4/page4-state1.html',
css: 'states/page4/page4-state1.css'
},
'state2': {
templateUrl: 'states/page4/page4-state2.html',
css: ['states/page4/page4-state2.css']
},
'state3': {
templateUrl: 'states/page4/page4-state3.html',
css: {
href: 'states/page4/page4-state3.css'
}
}
},
css: [
{
href: 'page4/page4.css',
}, {
href: 'page4/page4.mobile.css',
media: 'screen and (max-width : 768px)'
}, {
href: 'page4/page4.print.css',
media: 'print'
}
]
}); });
Responsive Design
AngularCSS supports "smart media queries". This means that stylesheets with media queries will be only added when the breakpoint matches. This will significantly optimize the load time of your apps.
$routeProvider
.when('/my-page', {
templateUrl: 'my-page/my-page.html',
css: [
{
href: 'my-page/my-page.mobile.css',
media: '(max-width: 480px)'
}, {
href: 'my-page/my-page.tablet.css',
media: '(min-width: 768px) and (max-width: 1024px)'
}, {
href: 'my-page/my-page.desktop.css',
media: '(min-width: 1224px)'
}
]
});
Even though you can use the media
property to specify media queries, the best way to manage your breakpoints is by settings them in the provider's default settings. For example:
myApp.config(function($routeProvider, $cssProvider) { angular.extend($cssProvider.defaults, {
breakpoints: {
mobile: '(max-width: 480px)',
tablet: '(min-width: 768px) and (max-width: 1024px)',
desktop: '(min-width: 1224px)'
}
}); $routeProvider
.when('/my-page', {
templateUrl: 'my-page/my-page.html',
css: [
{
href: 'my-page/my-page.mobile.css',
breakpoint: 'mobile'
}, {
href: 'my-page/my-page.tablet.css',
breakpoint: 'tablet'
}, {
href: 'my-page/my-page.desktop.css',
breakpoint: 'desktop'
}
]
}); });
Config
You can configure AngularCSS at the global or stylesheet level.
Configuring global options
These options are applied during your app's config
phase or via $cssProvider
.
myApp.config(function($cssProvider) { angular.extend($cssProvider.defaults, {
container: 'head',
method: 'append',
persist: false,
preload: false,
bustCache: false
}); });
Configuring CSS options
These options are applied at the stylesheet level.
css: {
href: 'file-path.css',
rel: 'stylesheet',
type: 'text/css',
media: false,
persist: false,
preload: false,
bustCache: false,
weight: 0
}
Support
AngularCSS is fully supported by AngularJS >= v1.3 && <= v1.5
There is partial support for AngularJS 1.2. It does not support css
property via DDO (Directive Definition Object). The workaround is to bind (or add) the CSS in the directive's controller or link function via $css
service.
myApp.directive('myDirective', function () {
return {
restrict: 'E',
templateUrl: 'my-directive/my-directive.html',
controller: function ($scope, $css) {
$css.bind('my-directive/my-directive.css', $scope);
}
}
});
Angular 2
Can I use AngularCSS in Angular 2?
AngularCSS is not necessary in Angular 2! Angular 2 ships with a similar feature out of the box. It's called styles
andstyleUrls
and it looks like this:
@Component({
selector: 'my-component',
templateUrl: 'app/components/my-component/my-component.html',
styleUrls: ['app/components/my-component/my-component.css'],
})
Browsers
Chrome, Firefox, Safari, iOS Safari, Android and IE9+
IE9 Does not support matchMedia API. This means that in IE9, stylesheets with media queries will be added without checking if the breakpoint matches.
Contributing
Please submit all pull requests the against master branch. If your pull request contains JavaScript patches or features, you should include relevant unit tests.
Copyright and license
The MIT License
Copyright (c) 2016 Alex Castillo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
文章来自:https://github.com/castillo-io/angular-css
AngularCSS--关于angularjs动态加载css文件的方法(仅供参考)的更多相关文章
- jQuery动态加载css文件实现方法
$("<link>").attr({ rel: "stylesheet",type: "text/css",href: &quo ...
- 两种动态加载JavaScript文件的方法
两种动态加载JavaScript文件的方法 第一种便是利用ajax方式,第二种是,动静创建一个script标签,配置其src属性,经过把script标签拔出到页面head来加载js,感乐趣的网友可以看 ...
- js动态加载css文件和js文件的方法
今天研究了下js动态加载js文件和css文件的方法. 网上发现一个动态加载的方法.摘抄下来,方便自己以后使用 [code lang="html"] <html xmlns=& ...
- 用JavaScript动态加载CSS和JS文件
本文转载自:http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/11/14/2248451.html 今天项目中需要用到动态加载 CSS 文件 ...
- .NET Web后台动态加载Css、JS 文件,换肤方案
后台动态加载文件代码: //假设css文件:TestCss.css #region 动态加载css文件 public void AddCss() { HtmlGenericControl _CssFi ...
- js实用方法记录-js动态加载css、js脚本文件
js实用方法记录-动态加载css/js 附送一个加载iframe,h5打开app代码 1. 动态加载js文件到head标签并执行回调 方法调用:dynamicLoadJs('http://www.yi ...
- JavaScript动态加载CSS和JS文件
var dynamicLoading = { css: function(path){ if(!path || path.length === 0){ throw new Error('argumen ...
- 动态加载css方法实现和深入解析
一.方法引用来源和应用 此动态加载css方法 loadCss,剥离自Sea.js,并做了进一步的优化(优化代码后续会进行分析). 因为公司项目需要用到懒加载来提高网站加载速度,所以将非首屏渲染必需 ...
- [AngularJS] 使用AngularCSS动态加载CSS
[AngularJS] 使用AngularCSS动态加载CSS 前言 使用AngularAMD动态加载Controller 使用AngularAMD动态加载Service 上列两篇文章里,介绍了如何如 ...
随机推荐
- MEF高级进阶
MEF高级进阶 好久没有写博客了,今天抽空继续写MEF系列的文章.有园友提出这种系列的文章要做个目录,看起来方便,所以就抽空做了一个,放到每篇文章的最后. 前面四篇讲了MEF的基础知识,学完了前四 ...
- Tabs - 选项卡插件
接上篇Tabs - 选项卡插件 其中12)Yet (E)Another Tab Interface没有依赖任何javascript框架,以作补充 9)Flipping C ...
- 使用UpdatePanel控件
使用UpdatePanel控件(二) UpdatePanel可以用来创建丰富的局部更新Web应用程序,它是ASP.NET 2.0 AJAX Extensions中很重要的一个控件,其强大之处在于不用编 ...
- Ninject依赖注入——构造函数、属性、方法和字段的注入
Ninject依赖注入——构造函数.属性.方法和字段的注入(三) 1.Ninject简介 Ninject是基于.Net平台的依赖注入框架,它能够将应用程序分离成一个个高内聚.低耦合(loosely-c ...
- [置顶] Hibernate从入门到精通(十)多对多单向关联映射
上一篇文章Hibernate从入门到精通(九)一对多双向关联映射中我们讲解了一下关于一对多关联映射的相关内容,这次我们继续多对多单向关联映射. 多对多单向关联映射 在讲解多对多单向关联映射之前,首先看 ...
- 利用redis的订阅和发布来实现实时监控的一个DEMO(Python版本)
redis的list类型有个很好的特性,就是每次添加元素后会返回当前list的长度,利用这个特点,我们可以监控它的长度,比如我们的key是用户注册的IP地址,list中存放的是已经在此IP地址上注册的 ...
- Load ContextCLR 探测
目录 背景Load ContextCLR 探测过程弱签名程序集的探测过程强签名程序集的探测过程Default ContextLoad-From ContextNo ContextRelfection- ...
- 在C#代码中应用Log4Net系列教程
在C#代码中应用Log4Net系列教程(附源代码) Log4Net应该可以说是DotNet中最流行的开源日志组件了.以前需要苦逼写的日志类,在Log4Net中简单地配置一下就搞定了.没用过Log4 ...
- [RM 状态机详解3]RMContainer状态机详解
摘要 RMContainer是RM内部维护的Container状态.事实上,在RM的调度器中,会维护着一个liveContainers列表,保存着所有存活着的Container信息.图1显示RMCon ...
- C#中鼠标划过按钮时候的提示信息
ToolTip p = new ToolTip(); p.ShowAlways = true; p.SetToolTip(this.Buton1, &quo ...