AngularJS的一些基本样式初窥

 更新时间:2015年07月27日 17:43:57   作者:humingx  
这篇文章主要介绍了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。

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

所有源码

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>angular demo</title>
  <script src="http://cdn.bootcss.com/angular.js/1.3.0-beta.8/angular.min.js"></script>
</head>
<body>
  <div id="App1" ng-app="shoppingCart" ng-controller="ShoppingCartController">
    <h1>Your demo</h1>
    <!-- demo 1 -->
    <div ng-show='menuState.show'>another another another</div>
    <button ng-click="test2()">切换</button>

    <hr><!-- demo 2 -->
    <style type="text/css">
      .menu-disabled-true{
        opacity:1;
        color: red;
        -webkit-transition:all 1000ms linear;
        -moz-transition:all 1000ms linear;
        -o-transition:all 1000ms linear;
      }
      .menu-disabled-false{
        opacity: 0;
        -webkit-transition:all 1000ms linear;
        -moz-transition:all 1000ms linear;
        -o-transition:all 1000ms linear;
      }
    </style>
    <div class="menu-disabled-{{isDisabled}}">adfadfadasda</div>
    <button ng-click="test()">隐藏</button>
    <button ng-click="test1()">显示</button>
    <button ng-click="test11()">切换</button>

    <hr><!-- demo 3 -->
    <style type="text/css">
    .error {
      background-color: red;
    }
    .warning {
      background-color: yellow;
    }
    </style>
    <div ng-class='{error:isError, warning:isWarning}'>{{messageText}}</div>
    <button ng-click="showError()">error</button>
    <button ng-click="showWarning()">warning</button>

    <hr><!-- demo 4 -->
    <style type="text/css">
      .selected{
        background-color: lightgreen;
      }
    </style>
    <div ng-repeat="item in items" ng-class='{selected:$index==selectedRow}' ng-click='selectedWhich($index)'>
      <span>{{item.product_name}}</span>
      <span>{{item.price | currency}}</span>
    </div>
  </div>

  <script>
    var shoppingCartModule = angular.module("shoppingCart", [])
    shoppingCartModule.controller("ShoppingCartController",
      function ($scope) {
        // demo 1
        $scope.menuState = {'show':true};
        $scope.test2 = function () {
          $scope.menuState.show = !$scope.menuState.show;
        };

        // demo 2
        $scope.isDisabled = true;
        $scope.test = function () {
          $scope.isDisabled = 'false';
        };
        $scope.test1 = function () {
          $scope.isDisabled = 'true';
        };
        $scope.test11 = function () {
          $scope.isDisabled = !$scope.isDisabled;
        };

        // demo 3
        $scope.isError = false;
        $scope.isWarning = false;
        $scope.messageText = 'default, default';
        $scope.showError = function () {
          $scope.messageText = 'This is an error';
          $scope.isError = true;
          $scope.isWarning = false;
        };
        $scope.showWarning = function () {
          $scope.messageText = 'Just a warning, donot warry';
          $scope.isWarning = true;
          $scope.isError = false;
        };

        // demo 4
        $scope.items = [
          { product_name: "Product 1", price: 50 },
          { product_name: "Product 2", price: 20 },
          { product_name: "Product 3", price: 180 }
        ];
        $scope.selectedWhich = function (row) {
          $scope.selectedRow = row;
        }
      }
    );
  </script>
</body>
</html>

相关文章

  • Angular1.x自定义指令实例详解

    Angular1.x自定义指令实例详解

    这篇文章主要介绍了Angular1.x自定义指令,结合实例形式分析了Angular1.x自定义指令的实现与使用方法,以及相关操作技巧与注意事项,需要的朋友可以参考下
    2017-03-03
  • AngularJS仿苹果滑屏删除控件

    AngularJS仿苹果滑屏删除控件

    前端开发中,为了对列表项进行快捷操作,有时就添个按钮来简单实现。但是,有时会发现按钮影响美观,甚至影响列表行的布局。稍在网上搜索无果,而写此仿苹果滑屏删除控件
    2016-01-01
  • 如何处理Angular 错误消息ERROR Error NullInjectorError No provider for XX

    如何处理Angular 错误消息ERROR Error NullInjectorError No provid

    这篇文章主要介绍了如何处理Angular 错误消息ERROR Error NullInjectorError No provider for XX
    2023-07-07
  • Angular环境搭建及简单体验小结

    Angular环境搭建及简单体验小结

    Angular基于TypeScript和react、vue相比 Angular更适合中大型企业级项目,本文通过实例代码给大家分享Angular环境搭建及简单体验,感兴趣的朋友跟随小编一起学习吧
    2021-05-05
  • AngularJS ng-bind-template 指令详解

    AngularJS ng-bind-template 指令详解

    本文注意介绍AngularJS ng-bind-template 指令,这里把相关资料整理了一下,并附实例代码,有需要的小伙伴可以参考下
    2016-07-07
  • AngularJS获取json数据的方法详解

    AngularJS获取json数据的方法详解

    这篇文章主要介绍了AngularJS获取json数据的方法,结合实例形式详细分析了AngularJS获取json数据的详细步骤、操作技巧与相关注意事项,需要的朋友可以参考下
    2017-05-05
  • AngularJs ng-route路由详解及实例代码

    AngularJs ng-route路由详解及实例代码

    这篇文章主要介绍了AngularJs ng-route路由,这里整理相关资料及简单实例代码,有兴趣的小伙伴可以参考下
    2016-09-09
  • Angularjs使用过滤器完成排序功能

    Angularjs使用过滤器完成排序功能

    这篇文章主要为大家详细介绍了Angularjs使用过滤器完成排序功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-09-09
  • 详解angular如何调用HTML字符串的方法

    详解angular如何调用HTML字符串的方法

    这篇文章主要介绍了详解angular如何调用HTML字符串的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-06-06
  • Angular Excel 导入与导出的实现代码

    Angular Excel 导入与导出的实现代码

    这篇文章主要介绍了Angular Excel 导入与导出的实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-04-04

最新评论