【AngularJS】—— 2 初識AngularJs(續)

前一篇瞭解了AngularJS的一些簡單的使用,這裏繼續跟着w3c學習一下剩下的內容。css

  本篇根據w3cschool.cc繼續學習AngularJS剩餘的內容,包括:html

  1 事件angularjs

  2 模塊web

  3 表單bootstrap

  4 數據驗證數組

  5 bootstrap CSS風格app

  6 include包含其餘頁面ide

  7 應用程序學習

  8 參考手冊ui

  

  首先看一下html的事件

  關於html的事件,文中給出了三個例子,點擊、隱藏、顯示。使用方法基本相同:

  先看一下點擊的例子,點擊按鈕後,會觸發ng-click內的方法,進行累計加一:

<div ng-app="" ng-controller="myController">
<button ng-click="count = count + 1">點我!</button>
<p>{{ count }}</p>
</div>

  對於ng-hide和ng-show原理相同,都是指定一個bool值,true或false。

  初始的時候myVar的值爲false,所以並不隱藏,每次點擊myVar都會改變爲相反的值,達到現實隱藏的效果。

<div ng-app="" ng-controller="personController">

<button ng-click="toggle()">隱藏/顯示</button>

<p ng-hide="myVar">
名: <input type="text" ng-model="person.firstName"><br>
姓: <input type="text" ng-model="person.lastName"><br>
<br>
姓名: {{person.firstName + " " + person.lastName}}
</p>

</div>

<script>
function personController($scope) {
    $scope.person = {
        firstName: "John",
        lastName: "Doe"
    };
    $scope.myVar = false;
    $scope.toggle = function() {
        $scope.myVar = !$scope.myVar;
    };
}
</script>

  ng-show使用方法也相同

<div ng-app="" ng-controller="personController">

<button ng-click="toggle()">隱藏/顯示</button>

<p ng-show="myVar">
名: <input type="text" ng-model="person.firstName"><br>
姓: <input type="text" ng-model="person.lastName"><br>
<br>
姓名: {{person.firstName + " " + person.lastName}}
</p>

</div>

<script>
function personController($scope) {
    $scope.person = {
        firstName: "John",
        lastName: "Doe"
    };
    $scope.myVar = true;
    $scope.toggle = function() {
        $scope.myVar = !$scope.myVar;
    };
}
</script>

 

  關於AngularJS的模塊

  模塊定義了用戶的應用,全部的控制器屬於一個模塊。以前使用的ng-app就是模塊的定義。

  例以下面的代碼,定義了一個應用程序的模塊myApp,並定義了一個控制器myCtrl:

<body>

<div ng-app="myApp" ng-controller="myCtrl">
{{ firstName + " " + lastName }}
</div>

<script src="//www.w3cschool.cc/try/angularjs/1.2.5/angular.min.js"></script>

<script src="myApp.js"></script>
<script src="myCtrl.js"></script>

</body>

  對於控制器,也能夠經過模塊來定義聲明:

<head>
<script src="//www.w3cschool.cc/try/angularjs/1.2.5/angular.min.js"></script>
</head>

<body>

<div ng-app="myApp" ng-controller="myCtrl">
{{ firstName + " " + lastName }}
</div>

<script>
var app = angular.module("myApp", []);

app.controller("myCtrl", function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
});
</script>

</body>

  不過要提早加載angularjs的腳本,這樣再script中 angular.module才能夠使用。或者在這句話的前面加載js。

 

  關於AngularJS表單

  表單是web中重要的組成部分,以下面樣例所示,能夠很方便的獲取到form中的數據

<div ng-app="" ng-controller="formController">
  <form novalidate>
    First Name:<br>
    <input type="text" ng-model="user.firstName"><br>
    Last Name:<br>
    <input type="text" ng-model="user.lastName">
    <br><br>
    <button ng-click="reset()">RESET</button>
  </form>
  <p>form = {{user}}</p>
  <p>master = {{master}}</p>
</div>

<script>
function formController ($scope) {
    $scope.master = {firstName: "John", lastName: "Doe"};
    $scope.reset = function() {
        $scope.user = angular.copy($scope.master);
    };
    $scope.reset();
};
</script>

 

  關於驗證

  AngularJS提供了不少驗證機制,例如郵箱等。

<!DOCTYPE html>
<html> 

<body>
<h2>Validation Example</h2>

<form  ng-app=""  ng-controller="validateCtrl"
name="myForm" novalidate>

<p>Username:<br>
  <input type="text" name="user" ng-model="user" required>
  <span style="color:red" ng-show="myForm.user.$dirty && myForm.user.$invalid">
  <span ng-show="myForm.user.$error.required">Username is required.</span>
  </span>
</p>

<p>Email:<br>
  <input type="email" name="email" ng-model="email" required>
  <span style="color:red" ng-show="myForm.email.$dirty && myForm.email.$invalid">
  <span ng-show="myForm.email.$error.required">Email is required.</span>
  <span ng-show="myForm.email.$error.email">Invalid email address.</span>
  </span>
</p>

<p>
  <input type="submit"
  ng-disabled="myForm.user.$dirty && myForm.user.$invalid ||
  myForm.email.$dirty && myForm.email.$invalid">
</p>

</form>

<script src="//apps.bdimg.com/libs/angular.js/1.2.15/angular.min.js"></script>
<script>
function validateCtrl($scope) {
    $scope.user = 'John Doe';
    $scope.email = 'john.doe@gmail.com';
}
</script>

</body>
</html>

  能夠經過.$dirty   .$invalid來驗證。

 

  添加bootstrap樣式

  能夠首先加載bootstrap css文件

<link rel="stylesheet" href="//apps.bdimg.com/libs/bootstrap/3.2.0/css/bootstrap.min.css">

  樣例代碼參考:

<!DOCTYPE html>
<html ang-app="">
<head>
<link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.2.0/css/bootstrap.min.css">
</head>

<body ng-controller="userController">
<div class="container">

<h3>Users</h3>

<table class="table table-striped">
  <thead><tr>
    <th>Edit</th>
    <th>First Name</th>
    <th>Last Name</th>
  </tr></thead>
  <tbody><tr ng-repeat="user in users">
    <td>
      <button class="btn" ng-click="editUser(user.id)">
      <span class="glyphicon glyphicon-pencil"></span>&nbsp;&nbsp;Edit
      </button>
    </td>
    <td>{{ user.fName }}</td>
    <td>{{ user.lName }}</td>
  </tr></tbody>
</table>

<hr>
<button class="btn btn-success" ng-click="editUser('new')">
  <span class="glyphicon glyphicon-user"></span> Create New User
</button>
<hr>

<h3 ng-show="edit">Create New User:</h3>
<h3 ng-hide="edit">Edit User:</h3>

<form class="form-horizontal">
<div class="form-group">
  <label class="col-sm-2 control-label">First Name:</label>
  <div class="col-sm-10">
    <input type="text" ng-model="fName" ng-disabled="!edit" placeholder="First Name">
  </div>
</div> 
<div class="form-group">
  <label class="col-sm-2 control-label">Last Name:</label>
  <div class="col-sm-10">
    <input type="text" ng-model="lName" ng-disabled="!edit" placeholder="Last Name">
  </div>
</div>
<div class="form-group">
  <label class="col-sm-2 control-label">Password:</label>
  <div class="col-sm-10">
    <input type="password" ng-model="passw1" placeholder="Password">
  </div>
</div>
<div class="form-group">
  <label class="col-sm-2 control-label">Repeat:</label>
  <div class="col-sm-10">
    <input type="password" ng-model="passw2" placeholder="Repeat Password">
  </div>
</div>
</form>

<hr>
<button class="btn btn-success" ng-disabled="error || incomplete">
  <span class="glyphicon glyphicon-save"></span> Save Changes
</button>
</div>

<script src = "http://apps.bdimg.com/libs/angular.js/1.2.15/angular.min.js"></script>
<script src = "myUsers.js"></script>
</body>
</html>
View Code

 

  關於include包含其餘html

  能夠經過ng-include來包含其餘的web頁面:

<body>
<div class="container">
  <div ng-include="'myUsers_List.htm'"></div>
  <div ng-include="'myUsers_Form.htm'"></div>
</div>
</body>

 

  關於應用程序

  建立AngularJS應用程序

<div ng-app="myTodoApp" ng-controller="myTodoCtrl">

<h2>個人筆記</h2>

<p><textarea ng-model="message" cols="40" rows="10"></textarea></p>

<p>
<button ng-click="save()">保存</button>
<button ng-click="clear()">清除</button>
</p>

<p>剩下的字符數:<span ng-bind="left()"></span></p>

</div>

<script src="myTodoApp.js"></script>
<script src="myTodoCtrl.js"></script>

 

  最後是Angular的指定 過濾器參考

  ng_app 定義應用程序

  ng_bind 綁定數據

  ng_click 定義元素單擊行爲

  ng_controller 定義控制器對象

  ng_disabled 定義html標籤可用

  ng_init 初始化數據

  ng_model 綁定應用程序數據

  ng_repeat 實現循環

  ng_show 顯示html標籤

 

  currency 轉換成貨幣

  filter 從數組中選擇一個子集

  lowercase 格式化字符串爲小寫

  uppercase 格式化字符串爲大寫

  orderBy 排序

相關文章
相關標籤/搜索