js架構——angular.js(4)

來源:互聯網
上載者:User

標籤:

1. angular中的對象

其實也不用多說的,前台是可以提取後台定義的對象的——

 1 <body ng-app="MyApp"> 2     <div ng-controller="User"> 3       <form ng-submit="submit()" novalidate> 4         <label>Firstname</label> 5         <input type="text" ng-model="user.firstname"/> 6         <label>Lastname</label> 7         <input type="text" ng-model="user.lastname"/> 8         <label>Age</label> 9         <input type="text" ng-model="user.age"/>10         <br>11         <button class="btn">Submit</button>12       </form>13 14       User Model: {{user}}15       <br>16       Form was submitted: {{wasSubmitted}}17     </div>18   </body>
 1 var app = angular.module("MyApp", []); 2  3 app.controller("User", function($scope) { 4   $scope.user = {a:4}; 5   $scope.wasSubmitted = false; 6  7   $scope.submit = function() { 8     $scope.wasSubmitted = true; 9   };10 });

上代碼中,可以看到14行的{{user}}成功提取了user對象裡的資料,而這個資料是由後台產生,前台的三個ng-model添加的。

 

 

2. 表單驗證

 $error

$error對象中儲存著沒有通過驗證的驗證器名稱以及對應的錯誤資訊。

 $pristine

$pristine的值是布爾型的,可以告訴我們使用者是否對控制項進行了修改。

 $dirty

$dirty的值和$pristine相反,可以告訴我們使用者是否和控制項進行過互動。

$valid

$valid值可以告訴我們當前的控制項中是否有錯誤。當有錯誤時值為false,沒有錯誤時值為true。

$invalid

$invalid值可以告訴我們當前控制項中是否存在至少一個錯誤,它的值和$valid相反。

 

常用的表單驗證指令 1. 必填項驗證

某個表單輸入是否已填寫,只要在輸入欄位元素上添加HTML5標記required即可:

<input type="text" required />  
2. 最小長度

驗證表單輸入的文本長度是否大於某個最小值,在輸入欄位上使用指令ng-minleng= "{number}":

<input type="text" ng-minlength="5" /> 
3. 最大長度

驗證表單輸入的文本長度是否小於或等於某個最大值,在輸入欄位上使用指令ng-maxlength="{number}":

<input type="text" ng-maxlength="20" />
4. 模式比對

使用ng-pattern="/PATTERN/"來確保輸入能夠匹配指定的Regex:

<input type="text" ng-pattern="/[a-zA-Z]/" /> 
5. 電子郵件

驗證輸入內容是否是電子郵件,只要像下面這樣將input的類型設定為email即可:

<input type="email" name="email" ng-model="user.email" /> 
6. 數字

驗證輸入內容是否是數字,將input的類型設定為number:

<input type="number" name="age" ng-model="user.age" /> 
7. URL

 驗證輸入內容是否是URL,將input的類型設定為 url:

 

 

<input type="url" name="homepage" ng-model="user.facebook_url" />
以上內容摘抄自:http://www.cnblogs.com/rohelm/p/4033513.html
作者寫的是真心不錯,這個頁面中還有相當多的執行個體,不懂的同學可以去看看

 

 

 

 

2. 驗證的運用

有了以上內容,我們就可以對錶單進行一些簡單的設定了。

angular中有個ng-show這個屬性,屬性值為true的時候,才會顯示這個標籤,和jquery中的show()非常類似。

於是,我們可以在標籤裡這麼寫——

<span class="help-block" ng-show="form.firstname.$invalid && form.firstname.$dirty">Firstname is required</span>

當form表單中,firstname發生更改, form.firstname.$dirty變為true,

當form表單中,firstname發生錯誤, form.firstname.$invalid變為true,

而兩個值都為ture的時候,ng-show才為true,顯示出來。

 

於是——當內容發生過了修改,但是內容仍然為空白,這個條件觸發的時候,上面這個span標籤才會顯示出來。

 

 

3. 欄位的選擇

當我們使用表單的時候,有些需要進行一些細節上的操作,例如,有些表單需要不驗證,有些需要必填。一般來說,只要在標籤後面添加對應屬性就可以了——

<input name="firstname" type="text" ng-model="user.firstname" required/>

最後的required代表必填

<form name="form" ng-submit="submit()" class="form-horizontal" novalidate novalidate>

代表表單提交的時候不需要驗證

 

js架構——angular.js(4)

聯繫我們

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