首页 > 语言 > JavaScript > 正文

AngularJS的一些基本样式初窥

2024-05-06 16:23:53
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了AngularJS的一些基本样式初窥,AngularJS是一款高人气JavaScript框架,需要的朋友可以参考下

显示和隐藏

在 Angular 中的一切,都是基于模型的改变,进而通过标识符反映这些变化到界面上。

ng-show 和 ng-hide 可以做相同的事情。显示和隐藏是基于你传递给他们的表达式而定,即,当表达式为 true 时,ng-show 就显示,反之隐藏。当表达式为 true 时,ng-hide 就隐藏,反之显示。这些标识符是通过设置元素的样式 display:block 显示和 display:none 隐藏进行工作的。

CSS类和样式

通过 {{}} 解析来进行数据绑定,从而能够动态地设置类和样式。

ng-class 和 ng-style

在大型项目中,上面的方式会使得难以管理,以至于不得不同时阅读模版和 JavaScript 才能正确地创建 css 。

Angular 提供了 ng-class 和 ng-style 标识符。他们每一个都需要一个表达式。表达式执行的结果可能是下列之一:

一个字符串,表示空间隔开的类名。

一个类名数组

一个类名到布尔值的映射

选中的行

模版中,我们设置 ng-class 的值为 {selected:$index==selectedRow},当模型调用selectedRow 时将匹配 ng-repeat 的 $index,进而显示选中的样式。同样我们设置 ng-click 来通知控制器用户点了哪一行。

src 和 href 建议

建议使用 ng-src 和 ng-href。

 

 
  1. <img ng-src="/img/01.png"> 
  2. <a ng-href="www.segmentfault.com">segmentfault</a> 

所有源码

 

 
  1. <!DOCTYPE html> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  5. <title>angular demo</title> 
  6. <script src="http://cdn.bootcss.com/angular.js/1.3.0-beta.8/angular.min.js"></script> 
  7. </head> 
  8. <body> 
  9. <div id="App1" ng-app="shoppingCart" ng-controller="ShoppingCartController"> 
  10. <h1>Your demo</h1> 
  11. <!-- demo 1 --> 
  12. <div ng-show='menuState.show'>another another another</div> 
  13. <button ng-click="test2()">切换</button> 
  14.  
  15. <hr><!-- demo 2 --> 
  16. <style type="text/css"> 
  17. .menu-disabled-true{ 
  18. opacity:1; 
  19. color: red; 
  20. -webkit-transition:all 1000ms linear; 
  21. -moz-transition:all 1000ms linear; 
  22. -o-transition:all 1000ms linear; 
  23. } 
  24. .menu-disabled-false{ 
  25. opacity: 0; 
  26. -webkit-transition:all 1000ms linear; 
  27. -moz-transition:all 1000ms linear; 
  28. -o-transition:all 1000ms linear; 
  29. } 
  30. </style> 
  31. <div class="menu-disabled-{{isDisabled}}">adfadfadasda</div> 
  32. <button ng-click="test()">隐藏</button> 
  33. <button ng-click="test1()">显示</button> 
  34. <button ng-click="test11()">切换</button> 
  35.  
  36. <hr><!-- demo 3 --> 
  37. <style type="text/css"> 
  38. .error { 
  39. background-color: red; 
  40. } 
  41. .warning { 
  42. background-color: yellow; 
  43. } 
  44. </style> 
  45. <div ng-class='{error:isError, warning:isWarning}'>{{messageText}}</div> 
  46. <button ng-click="showError()">error</button> 
  47. <button ng-click="showWarning()">warning</button> 
  48.  
  49. <hr><!-- demo 4 --> 
  50. <style type="text/css"> 
  51. .selected{ 
  52. background-color: lightgreen; 
  53. } 
  54. </style> 
  55. <div ng-repeat="item in items" ng-class='{selected:$index==selectedRow}' ng-click='selectedWhich($index)'> 
  56. <span>{{item.product_name}}</span> 
  57. <span>{{item.price | currency}}</span> 
  58. </div> 
  59. </div> 
  60.  
  61. <script> 
  62. var shoppingCartModule = angular.module("shoppingCart", []) 
  63. shoppingCartModule.controller("ShoppingCartController", 
  64. function ($scope) { 
  65. // demo 1 
  66. $scope.menuState = {'show':true}; 
  67. $scope.test2 = function () { 
  68. $scope.menuState.show = !$scope.menuState.show; 
  69. }; 
  70.  
  71. // demo 2 
  72. $scope.isDisabled = true; 
  73. $scope.test = function () { 
  74. $scope.isDisabled = 'false'; 
  75. }; 
  76. $scope.test1 = function () { 
  77. $scope.isDisabled = 'true'; 
  78. }; 
  79. $scope.test11 = function () { 
  80. $scope.isDisabled = !$scope.isDisabled; 
  81. }; 
  82.  
  83. // demo 3 
  84. $scope.isError = false; 
  85. $scope.isWarning = false; 
  86. $scope.messageText = 'default, default'; 
  87. $scope.showError = function () { 
  88. $scope.messageText = 'This is an error'; 
  89. $scope.isError = true; 
  90. $scope.isWarning = false; 
  91. }; 
  92. $scope.showWarning = function () { 
  93. $scope.messageText = 'Just a warning, donot warry'; 
  94. $scope.isWarning = true; 
  95. $scope.isError = false; 
  96. }; 
  97.  
  98. // demo 4 
  99. $scope.items = [ 
  100. { product_name: "Product 1", price: 50 }, 
  101. { product_name: "Product 2", price: 20 }, 
  102. { product_name: "Product 3", price: 180 } 
  103. ]; 
  104. $scope.selectedWhich = function (row) { 
  105. $scope.selectedRow = row; 
  106. } 
  107. } 
  108. ); 
  109. </script> 
  110. </body> 
  111. </html> 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

图片精选