angualarjsdemo
AngularJs學(xué)習(xí)筆記--Forms
原版地址:http://code.angularjs.org/1.0.2/docs/guide/forms
?
控件(input、select、textarea)是用戶(hù)輸入數(shù)據(jù)的一種方式。Form(表單)是這些控件的集合,目的是將相關(guān)的控件進(jìn)行分組。
表單和控件提供了驗(yàn)證服務(wù),所以用戶(hù)可以收到無(wú)效輸入的提示。這提供了更好的用戶(hù)體驗(yàn),因?yàn)橛脩?hù)可以立即獲取到反饋,知 道如何修正錯(cuò)誤。請(qǐng)記住,雖然客戶(hù)端驗(yàn)證在提供良好的用戶(hù)體驗(yàn)中扮演重要的角色,但是它可以很簡(jiǎn)單地被繞過(guò),因此,客戶(hù)端驗(yàn)證是不可信的。服務(wù)端驗(yàn)證對(duì)于 一個(gè)安全的應(yīng)用來(lái)說(shuō)仍然是必要的。
一、Simple?form
理解雙向數(shù)據(jù)綁定的關(guān)鍵directive是ngModel。ngModel提供了從model到view和view到model的雙向數(shù)據(jù)綁定。并且,它還向其他directive提供API,增強(qiáng)它們的行為。
二、Using?CSS?classes
為了讓form以及控件、ngModel富有樣式,可以增加以下class:
- ng-valid
- ng-invalid
- ng-pristine(從未輸入過(guò))
- ng-dirty(輸入過(guò))
下面的例子,使用CSS去顯示每個(gè)表單控件的有效性。例子中,user.name和user.email都是必填的,但當(dāng)它們修改過(guò)之后(dirty),背景將呈現(xiàn)紅色。這確保用戶(hù)不會(huì)直到與表單交互之后(提交之后?),發(fā)現(xiàn)未能滿(mǎn)足其有效性,才為一個(gè)錯(cuò)誤而分心。
<!DOCTYPE HTML> <html lang="zh-cn" ng-app="CSSClasses"> <head> <meta charset="UTF-8"> <title>CSSClasses</title> <style type="text/css"> .ng-cloak { display: none; } .css-form input.ng-invalid.ng-dirty { background-color: #fa787e; } .css-form input.ng-valid.ng-dirty { background-color: #78fa89; } </style> </head> <body> <div ng-controller="MyCtrl" class="ng-cloak"> <form novalidate class="css-form" name="formName"> 名字: <input ng-model="user.name" type="text" required><br/> Email: <input ng-model="user.email" type="email" required><br/> 性別: <input value="男" ng-model="user.gender" type="radio">男 <input value="女" ng-model="user.gender" type="radio">女 <br/> <button ng-click="reset()">RESET</button> <button ng-click="update(user)">SAVE</button> </form> <pre>form = {{user | json}}</pre> <pre>saved = {{saved | json}}</pre> </div> <script src="../angular-1.0.1.js" type="text/javascript"></script> <script type="text/javascript"> var app = angular.module("CSSClasses", []); app.controller("MyCtrl", function ($scope,$window) { $scope.saved = {}; $scope.update = function(user) { $scope.saved = angular.copy(user); }; $scope.reset = function() { $scope.user = angular.copy($scope.saved); }; $scope.reset(); //不合法的值將不會(huì)進(jìn)入user }); </script> </body> </html>?
三、Binding?to?form?and?control?state
在angular中,表單是FormController的一個(gè)實(shí)例。表單實(shí)例可以隨意地使用name屬性暴露到scope中(這里沒(méi)看懂,scope里面沒(méi)有一個(gè)跟form的name屬性一直的property,但由于有“document.表單名”這種方式,所以還是可以獲取到的)。相似地,控件是NgModelController的實(shí)例??丶?shí)例可以相似地使用name屬性暴露在form中。這說(shuō)明form和控件(control)兩者的內(nèi)部屬性對(duì)于使用標(biāo)準(zhǔn)綁定實(shí)體(standard?binding?primitives)綁定在視圖中的這個(gè)做法是可行的。
這允許我們通過(guò)以下特性來(lái)擴(kuò)展上面的例子:
- RESET按鈕僅僅在form發(fā)生改變之后才可用。
- SAVE按鈕僅僅在form發(fā)生改變而且輸入有效的情況下可用。
- 為user.email和user.agree定制錯(cuò)誤信息。
?
四、Custom?Validation
angular為大多數(shù)公共的HTML表單域(input,text,number,url,email,radio,checkbox)類(lèi)型提供了實(shí)現(xiàn),也有一些為了表單驗(yàn)證的directive(required,pattern,,inlength,maxlength,min,max)。
可以通過(guò)定義增加定制驗(yàn)證函數(shù)到ngModel?controller(這里是連在一起的ngModelController嗎?)中的directive來(lái)定義我們自己的驗(yàn)證插件。為了得到一個(gè)controller,directive如下面的例子那樣指定了依賴(lài)(directive定義對(duì)象中的require屬性)。
- Model到View更新?-?無(wú)論什么時(shí)候Model發(fā)生改變,所有在ngModelController.$formatters(當(dāng)model發(fā)生改變時(shí)觸發(fā)數(shù)據(jù)有效性驗(yàn)證和格式化轉(zhuǎn)換)數(shù)組中的function將排隊(duì)執(zhí)行,所以在這里的每一個(gè)function都有機(jī)會(huì)去格式化model的值,并且通過(guò)NgModelController.$setValidity(http://code.angularjs.org/1.0.2/docs/api/ng.directive:ngModel.NgModelController#$setValidity)修改控件的驗(yàn)證狀態(tài)。
- View到Model更新?-?一個(gè)相似的方式,無(wú)論任何時(shí)候,用戶(hù)與一個(gè)控件發(fā)生交互,將會(huì)觸發(fā)NgModelController.$setViewValue。這時(shí)候輪到執(zhí)行NgModelController$parsers(當(dāng)控件從DOM中取得值之后,將會(huì)執(zhí)行這個(gè)數(shù)組中所有的方法,對(duì)值進(jìn)行審查過(guò)濾或轉(zhuǎn)換,也進(jìn)行驗(yàn)證)數(shù)組中的所有方法。
在下面的例子中我們將創(chuàng)建兩個(gè)directive。
- 第一個(gè)是integer,它負(fù)責(zé)驗(yàn)證輸入到底是不是一個(gè)有效的整數(shù)。例如1.23是一個(gè)非法的值,因?yàn)樗?shù)部分。注意,我們通過(guò)在數(shù)組頭部插入(unshift)來(lái)代替在尾部插入(push),這因?yàn)槲覀兿胨紫葓?zhí)行并使用這個(gè)控件的值(估計(jì)這個(gè)Array是當(dāng)作隊(duì)列來(lái)使用的),我們需要在轉(zhuǎn)換發(fā)生之前執(zhí)行驗(yàn)證函數(shù)。
- 第二個(gè)directive是smart-float。他將”1.2”和”1,2”轉(zhuǎn)換為一個(gè)合法的浮點(diǎn)數(shù)”1.2”。注意,我們?cè)谶@不可以使用HTML5的input類(lèi)型”number”,因?yàn)闉g覽器不允許用戶(hù)輸入我們預(yù)期的非法字符,如”1,2”(它只認(rèn)識(shí)”1.2”)。
?
五、Implementing?custom?form?control?(using?ngModel)
angular實(shí)現(xiàn)了所有HTML的基礎(chǔ)控件(input,select,textarea),能勝任大多數(shù)場(chǎng)景。然而,如果我們需要更加靈活,我們可以通過(guò)編寫(xiě)一個(gè)directive來(lái)實(shí)現(xiàn)自定義表單控件的目的。
為了制定控件和ngModel一起工作,并且實(shí)現(xiàn)雙向數(shù)據(jù)綁定,它需要:
- 實(shí)現(xiàn)render方法,是負(fù)責(zé)在執(zhí)行完并通過(guò)所有NgModelController.$formatters方法后,呈現(xiàn)數(shù)據(jù)的方法。
- 調(diào)用$setViewValue方法,無(wú)論任何時(shí)候用戶(hù)與控件發(fā)生交互,model需要進(jìn)行響應(yīng)的更新。這通常在DOM事件監(jiān)聽(tīng)器里實(shí)現(xiàn)。
可以查看$compileProvider.directive(http://www.cnblogs.com/lcllao/archive/2012/09/09/2677190.html)獲取更多信息。
下面的例子展示如何添加雙向數(shù)據(jù)綁定特性到可以編輯的元素中。
?
<!DOCTYPE HTML> <html lang="zh-cn" ng-app="CustomControl"> <head> <meta charset="UTF-8"> <title>CustomControl</title> <style type="text/css"> .ng-cloak { display: none; } div[contenteditable] { cursor: pointer; background-color: #D0D0D0; } </style> </head> <body ng-controller="MyCtrl"> <div class="ng-cloak"> <div contenteditable="true" ng-model="content" title="點(diǎn)擊后編輯">My Little Dada</div> <pre>model = {{content}}</pre> <button ng-click="reset()">reset model tirgger model to view($render)</button> </div> <script src="../angular-1.0.1.js" type="text/javascript"></script> <script type="text/javascript"> var app = angular.module("CustomControl", []); app.controller("MyCtrl", function ($scope) { $scope.reset = function() { $scope.content = "My Little Dada"; //在這里如何獲取NgModelController呢?如果你們知道,希望可以指點(diǎn)指點(diǎn)!謝謝 }; }); app.directive("contenteditable", function () { return { require:"ngModel", link:function (scope, ele, attrs, ctrl) { //view -> model ele.bind("blur keyup",function() { scope.$apply(function() { console.log("setViewValue"); ctrl.$setViewValue(ele.text()); }); }); //model -> view ctrl.$render = function(value) { console.log("render"); ele.html(value); }; //讀取初始值 ctrl.$setViewValue(ele.text()); } }; }); </script> </body> </html>轉(zhuǎn)載于:https://www.cnblogs.com/Jerry-spo/p/5689692.html
總結(jié)
以上是生活随笔為你收集整理的angualarjsdemo的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 2018java基础面试题(我自己用的,
- 下一篇: 分珠(dfs+并查集)