24-表單


<!--實例解析

ng-app 指令定義了 AngularJS 應用。

ng-controller 指令定義了應用控制器。

ng-model 指令綁定了兩個 input 元素到模型的 user 對象。

formCtrl 函數設置了 master 對象的初始值,並定義了 reset() 方法。

reset() 方法設置了 user 對象等於 master 對象。

ng-click 指令調用了 reset() 方法,且在點擊按鈕時調用。

novalidate 屬性在應用中不是必須的,但是你需要在 AngularJS 表單中使用,用於重寫標準的 HTML5 驗證。 -->
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<meta charset="utf-8">
<script src="js/angular.js"></script>
</head>
<body>
<!-- Checkbox(單選框)
我們可以使用 ng-model 來綁定單選按鈕到你的應用中。
單選框使用同一個 ng-model ,可以有不同的值,但只有被選中的單選按鈕的值會被使用
-->
<form>
  選擇一個選項:
  <input type="radio" ng-model="myVar" value="dogs">Dogs
  <input type="radio" ng-model="myVar" value="tuts">Tutorials
  <input type="radio" ng-model="myVar" value="cars">Cars
</form>
<div ng-switch="myVar">
  <div ng-switch-when="dogs">
     <h1>Dogs</h1>
     <p>Welcome to a world of dogs.</p>
  </div>
  <div ng-switch-when="tuts">
     <h1>Tutorials</h1>
     <p>Learn from examples.</p>
  </div>
  <div ng-switch-when="cars">
     <h1>Cars</h1>
     <p>Read about cars.</p>
  </div>
</div>
<p>ng-switch 指令根據單選按鈕的選擇結果顯示或隱藏 HTML 區域。</p><br><br><br><br>

<!-- 下拉菜單
使用 ng-model 指令可以將下拉菜單綁定到你的應用中。
ng-model 屬性的值爲你在下拉菜單選中的選項
-->
<form>
  選擇一個選項:
  <select ng-model="myVar2">
    <option value="">
    <option value="dogs">Dogs
    <option value="tuts">Tutorials
    <option value="cars">Cars
  </select>
</form>
<div ng-switch="myVar2">
  <div ng-switch-when="dogs">
     <h1>Dogs</h1>
     <p>Welcome to a world of dogs.</p>
  </div>
  <div ng-switch-when="tuts">
     <h1>Tutorials</h1>
     <p>Learn from examples.</p>
  </div>
  <div ng-switch-when="cars">
     <h1>Cars</h1>
     <p>Read about cars.</p>
  </div>
</div>
<p>ng-switch 指令根據下拉菜單的選擇結果顯示或隱藏 HTML 區域。</p><br><br><br><br>

<!-- novalidate-->
<form action="xxx.do" novalidate>
E-mail: <input type="email" name="user_email">
<input type="submit">
</form>
<p><strong>注意:</strong>在 Safari 和 Internet Explorer 9 及之前的版本中不支持 novalidate 屬性。這個屬性可以關閉瀏覽器默認校驗</p><br><br><br><br>


<!--Checkbox(複選框)
checkbox 的值爲 true 或 false,可以使用 ng-model 指令綁定,它的值可以用於應用中-->
<div ng-app="">
  <form>
    選中複選框,顯示標題:
    <input type="checkbox" ng-model="myVar">
  </form>
  <h1 ng-show="myVar">My Header</h1>
</div>
<p>標題使用了 ng-show 指令,複選框選中後顯示 h1 標籤內容。</p><br><br><br><br>

<!-- HTML 表單
HTML 表單通常與 HTML 控件同時存在
以下 HTML input 元素被稱爲 HTML 控件:

    input 元素
    select 元素
    button 元素
    textarea 元素
-->
<div ng-app="myApp" ng-controller="formCtrl">
  <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>

var app = angular.module('myApp', []);
app.controller('formCtrl', function($scope) {
    $scope.master = {firstName: "John", lastName: "Doe"};
    $scope.reset = function() {

        $scope.user = angular.copy($scope.master);

    };
    $scope.reset();
});

</script>
</body>
</html>
發佈了63 篇原創文章 · 獲贊 4 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章