国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

AngularJS使用angular-formly進行表單驗證

2019-11-20 10:54:46
字體:
來源:轉載
供稿:網友

當驗證表單中有很多字段時,這時候可能希望把html的生成以及驗證邏輯放到controller中,在頁面,也許是這樣的:

<some-form fiedls="vm.someFields" ...></some-form>

然后,在controller中定義各個字段以及驗證。angular-formly就是為這個需求而存在。

在controller中,把各個字段定義在數組中:

vm.rentalFields = [{key:'first_name',type:'input',templateOptions:{type:'text',label:'姓',placeholder: '輸入姓',required: true}},...]

使用hideExpression字段定義隱藏的條件:

{key:'under18',type:'checkbox',templateOptions:{label:'是否不滿18歲'},hideExpression: '!model.email' //email驗證失敗之前不顯示}

使用validators字段自定義驗證規則:

{key:'license',type:'input',templateOptions:{label:'身份證號',placeholder:'輸入身份證號'},hideExpression: '!model.province',validators:{driversLicense: function($viewValue, $modelValue, scope){var value = $modelValue || $viewValue;if(value){return validateDriversLicence(value);}},expressionProperties:{'templateOptions.disabled':function($viewValue, $modelValue, scope){if(scope.model.province == '山東省'){return false;}return true;}}}

首先安裝:npm install angular-formly angular-formly-templates-bootstrap bootstrap api-check

Demo的文件結構:

css/
.....style.css
node_modules/
scripts/
.....MainController.js
.....provinces.js [提供select的選項,有關省]

app.js

index.html

index.html

<!DOCTYPE html><html lang="en"><head><meta charset="gb2312"><title></title><link rel="stylesheet" href="css/style.css"/><link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css"/></head><body ng-app="formlyApp" ng-controller="MainController as vm"><div class="container col-md-4 col-md-offset-4"><form novalidate><formly-form model="vm.rental" fields="vm.rentalFields" form="vm.rentalForm"><button type="submit" class="btn btn-primary" ng-disabled="vm.rentalForm.$invalid">提交</button></formly-form></form></div><!--項目依賴--><script src="node_modules/api-check/dist/api-check.js"></script><script src="node_modules/angular/angular.min.js"></script><script src="node_modules/angular-formly/dist/formly.js"></script><script src="node_modules/angular-formly-templates-bootstrap/dist/angular-formly-templates-bootstrap.min.js"></script><!--項目引用--><script src="app.js"></script><script src="scripts/MainController.js"></script><script src="scripts/province.js"></script></body></html>

app.js

(function(){'use strict';angular.module('formlyApp',['formly','formlyBootstrap'])})();

province.js

以factory的方式返回一個對象,包含獲取select選項的方法。

(function(){'use strict';

angular

.module('formlyApp').factory('province', province);function province(){function getProvinces(){return [{"name":"山東省","value":"山東省"},{"name":"江蘇省","value":"江蘇省"}];}return {getProvinces:getProvinces}}})();

MainController.js

(function(){'use strict';angular.module('formlyApp').controller('MainController', MainController);function MainController(province){var vm = this;vm.rental = {};vm.rentalFields = [{key:'first_name',type:'input',templateOptions:{type:'text',label:'姓',placeholder: '輸入姓',required: true}},{key:'last_name',type:'input',templateOptions:{type:'text',label:'名',placeholder:'輸入名',required:true}},{key:'email',type:'input',templateOptions:{type:'email',label:'郵箱',placeholder:'輸入郵箱',required:true}},{key:'under18',type:'checkbox',templateOptions:{label:'是否不滿18歲'},hideExpression: '!model.email' //email驗證失敗之前不顯示},{key: 'province',type:'select',templateOptions:{label:'選擇省',options: province.getProvinces()},hideExpression: '!model.email'},{key:'license',type:'input',templateOptions:{label:'身份證號',placeholder:'輸入身份證號'},hideExpression: '!model.province',validators:{driversLicense: function($viewValue, $modelValue, scope){var value = $modelValue || $viewValue;if(value){return validateDriversLicence(value);}},expressionProperties:{'templateOptions.disabled':function($viewValue, $modelValue, scope){if(scope.model.province == '山東省'){return false;}return true;}}}},{key: 'insurance',type: 'input',templateOptions:{label:'保險',placeholder:'輸入保險'},hideExpression: '!model.under18 || !model.province'}];function validateDriversLicence(value) {return /[A-Za-z]/d{4}[/s|/-]*/d{5}[/s|/-]*/d{5}$/.test(value);}}})();

以上內容是小編給大家分享的AngularJS使用angular-formly進行表單驗證的全部敘述,希望大家喜歡。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东城区| 同仁县| 文安县| 东港市| 揭阳市| 贡嘎县| 进贤县| 屯留县| 昌邑市| 阳东县| 万安县| 宽甸| 淳安县| 恩施市| 武陟县| 临朐县| 华阴市| 颍上县| 旬阳县| 安化县| 安西县| 济阳县| 玉林市| 延川县| 望城县| 大埔县| 兴和县| 北川| 赣榆县| 云和县| 镇远县| 双柏县| 汉源县| 乐安县| 伊宁市| 无为县| 元江| 靖宇县| 宽城| 沙坪坝区| 宿松县|