【AngularJS】—— 5 表單

來源:互聯網
上載者:User

標籤:

這部分,我們寫一個表單程式,使用angularjs的檢測並完成表單屬性的擷取與拷貝。

  在AngularJS中,也支援html5中多種控制項的自動檢測,如:text、number、url、email、radio、checkbox等等。

  可以通過anuglar.copy()自動拷貝form表單的資料。

  代碼參考如下:

<!doctype html><html ng-app>    <head>         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />         <script src="http://apps.bdimg.com/libs/angular.js/1.2.16/angular.min.js"></script>    </head>    <body>        <div ng-controller="controller">            <form novalidate class="css-form">                Name: <input type="text" ng-model="user.name" required /><br>                E-mail: <input type="email" ng-model="user.email" required /><br>                Age: <input type="number" ng-model="user.age" min="1" max="99" required /><br>                Gender: <input type="radio" ng-model="user.gender" value="male"/>male                    <input type="radio" ng-model="user.gender" value="female" />female<br>                <button ng-click="reset()">reset</button>                <button ng-click="update(user)">save</button>            </form>            <pre>form = {{user | json}}</pre>            <pre>master = {{master | json}}</pre>        </div>        <style type="text/css">            .css-form input.ng-invalid.ng-dirty {                background-color: #FA787E;            }            .css-form input.ng-valid.ng-dirty {                background-color: #78FA89;            }        </style>        <script type="text/javascript">            function controller($scope){                $scope.master = {};                $scope.update = function(user){                    $scope.master = angular.copy(user);                };                $scope.reset = function(){                    $scope.user = angular.copy($scope.master);                };                $scope.reset();            }        </script>    </body></html>

  當表單資料填寫正確時,背景現實綠色,當資料非法時,則現實紅色。

  使用save則把form中的資料儲存到master中,使用reset就把master中的資料拷貝到form中。

  運行結果參考:

  

【AngularJS】—— 5 表單

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.