首頁  >  文章  >  web前端  >  AngularJS的一些基本樣式初窺_AngularJS

AngularJS的一些基本樣式初窺_AngularJS

WBOY
WBOY原創
2016-05-16 15:48:401172瀏覽

顯示與隱藏

在 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>

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn