Angularcss
CSS点播AngularJS
通过需要动态注入样式表,优化单页应用程序的演示层。
AngularCSS聆听路由(或状态)更改事件,添加了当前路由上定义的CSS,并从以前的路由中删除CSS。它还以与编译和范围销毁事件相同的方式与指令一起使用。有关更多详细信息,请参见下面的代码样本。
阅读文章
引入AngularCS:AngularJS的CSS点播
演示
Angular的Ngroute演示(来源)
UI路由器演示(来源)
快速开始
使用Bower或JSPM安装和管理。 CDNJS.COM也提供了CDN
$ bower install angular-css
$ jspm install github:castillo-io/angular-css
- 在您的
index.html中包括所需的JavaScript库(ngroute和UI路由器是可选的)。
< 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 >
- 将
angularCSS添加为您的应用程序的依赖性。
var myApp = angular . module ( \'myApp\' , [ \'ngRoute\' , \'angularCSS\' ] ) ;
注意:模块名称“ door3.css”现在已弃用。
例子
该模块可以通过在路由值,指令中添加CSS属性或调用控制器和服务的$css服务方法来使用该模块。
CSS属性支持一个字符串,一系列字符串,对象符号或对象数组。
有关更多信息,请参见下面的示例。
在组件中
myApp . component ( \'myComponent\' , { css : \'my-component/my-component.css\' // <--- magic! templateUrl : \'my-component/my-component.html\' , } ) ;
在指令中
myApp . directive ( \'myDirective\' , function ( ) { return { restrict : \'E\' , templateUrl : \'my-directive/my-directive.html\' , /* Binding css to directives */ css : \'my-directive/my-directive.css\' } } ) ;
在控制器中
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 ( ) ; } ) ;
对于路线(Angular的Ngroute)
需要ngroute作为依赖关系
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\' } ] } ) ; } ) ;
对于州(UI路由器)
需要ui.Router作为依赖关系
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\' } ] } ) ; } ) ;
响应式设计
AngularCSS支持“智能媒体查询”。这意味着只有在断点匹配时,才会添加带有媒体查询的样式表。这将大大优化应用程序的负载时间。
$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)\' } ] } ) ;
即使您可以使用media属性来指定媒体查询,但管理断点的最佳方法是通过在提供商的默认设置中进行设置。例如:
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
您可以在全局或样式表级别配置AngularCS。
配置全局选项
这些选项是在应用程序的config阶段或通过$cssProvider应用的。
myApp . config ( function ( $cssProvider ) { angular . extend ( $cssProvider . defaults , { container : \'head\' , method : \'append\' , persist : false , preload : false , bustCache : false } ) ; } ) ;
配置CSS选项
这些选项应用于样式表级别。
css: { href : \'file-path.css\' , rel : \'stylesheet\' , type : \'text/css\' , media : false , persist : false , preload : false , bustCache : false , weight : 0 }
支持
AngularCSS完全由AngularJS> = V1.3 && <= V1.5支持
对AngularJS 1.2有部分支持。它不通过DDO(指示定义对象)支持css属性。解决方法是通过$css服务在指令的控制器或链接功能中绑定(或添加)CSS。
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 ) ; } } } ) ;
角2
我可以在Angular 2中使用AngularCS吗?
在Angular 2中不需要AngularCSS! Angular 2船具有类似的功能。它被称为styles和styleUrls ,看起来像这样:
@ Component ( { selector : \'my-component\' , templateUrl : \'app/components/my-component/my-component.html\' , styleUrls : [ \'app/components/my-component/my-component.css\' ] , } )
浏览器
Chrome,Firefox,Safari,Ios Safari,Android和IE9+
IE9不支持MatchMedia API。这意味着在IE9中,将添加带有媒体查询的样式表,而无需检查断点是否匹配。
贡献
请提交所有拉动请求,反对主分支。如果您的拉请求包含JavaScript补丁或功能,则应包括相关的单元测试。
版权和许可
The MIT License
Copyright (c) 2017 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.
