让Angularjs外面表单忽略对内嵌表单的校验
来源:互联网 发布:赫连勃勃大王知乎 编辑:程序博客网 时间:2024/05/18 04:46
从stackoverflow中收到的解答,地址http://stackoverflow.com/questions/19333544/skip-nested-forms-validation-with-angularjs。
自定义isolate-form directive作为内嵌的表单,能够绕过外面表单的验证。customed directives as follows:
angular.module('isolateForm',[]).directive('isolateForm', [function () { return { restrict: 'A', require: '?form', link: function (scope, elm, attrs, ctrl) { if (!ctrl) { return; } // Do a copy of the controller var ctrlCopy = {}; angular.copy(ctrl, ctrlCopy); // Get the parent of the form var parent = elm.parent().controller('form'); // Remove parent link to the controller parent.$removeControl(ctrl); // Replace form controller with a "isolated form" var isolatedFormCtrl = { $setValidity: function (validationToken, isValid, control) { ctrlCopy.$setValidity(validationToken, isValid, control); parent.$setValidity(validationToken, true, ctrl); }, $setDirty: function () { elm.removeClass('ng-pristine').addClass('ng-dirty'); ctrl.$dirty = true; ctrl.$pristine = false; }, }; angular.extend(ctrl, isolatedFormCtrl); } };}]);
To use it just call the directive "isolate-form" :
<form name="parent"> <input type="text" ng-model="outside"/> <ng-form name="subform" isolate-form> <input type="text" ng-model="inside"/> </ng-form></form>
0 0
- 让Angularjs外面表单忽略对内嵌表单的校验
- angularjs简单表单校验
- angularJs表单校验
- AngularJS表单校验
- AngularJS 学习笔记(表单校验篇)
- AngularJS 学习笔记(表单校验篇)
- Angularjs使用指令做表单校验
- AngularJs轻松入门(六)表单校验
- angularjs input 表单校验指令整理
- angularJs表单校验(超级详细!!!)
- Struts2的表单校验
- Angularjs表单的使用
- AngularJS 的表单验证
- AngularJS 的表单验证
- AngularJS 神奇的表单
- AngularJs的表单验证
- angularJs的表单验证
- AngularJS的表单交互
- 腾讯面试题练习
- 协方差、相关系数---通俗解释
- jQuery源码解析(架构与依赖模块)
- WV.27-大数阶乘算法7-入门篇之二
- HDU2212 DFS【水题】【打表】
- 让Angularjs外面表单忽略对内嵌表单的校验
- 1023. Have Fun with Numbers (20)
- iOS视图切换动画
- 1024. Palindromic Number (25)
- Android中SwipeRefreshLayout和listview的冲突解决办法
- Raspberry Pi -- Cross Compiling on Mac OSX
- WCF学习之基本概念的理解
- Android 控制ScrollView滚动到底部
- WV.28-大数阶乘算法8-入门篇之三汇编的威力