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

來源:互聯網
上載者:User

標籤:

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

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

  1 事件

  2 模組

  3 表單

  4 資料驗證

  5 bootstrap CSS風格

  6 include包含其他頁面

  7 應用程式

  8 參考手冊

  

  首先看一下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 = ‘[email protected]‘;}</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 排序

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.