让Angularjs外面表单忽略对内嵌表单的校验
2015-01-30 22:05
323 查看
从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>
自定义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>
相关文章推荐
- angularJs表单校验(超级详细!!!)
- AngularJS学习笔记五 表单校验
- angularjs简单表单校验
- angularjs 表单校验
- Angularjs使用指令做表单校验
- angularJs表单校验
- AngularJs轻松入门(六)表单校验
- JavaScript框架之AngularJS学习——表单校验
- AngularJs轻松入门(六)表单校验
- Angularjs使用指令做表单校验的方法
- AngularJs轻松入门(六)表单校验
- AngularJs表单校验功能实例代码
- AngularJS入门教程之表单校验用法示例
- angularJs表单校验(超级详细!!!)
- angularjs input 表单校验指令整理
- 深究AngularJS——校验(非form表单)
- AngularJS - 表单校验信息提示
- AngularJS表单校验
- AngularJs实现ng1.3+表单验证
- angularjs 之表单ng-form