AngularJS - 在控制器中的ng-repeat undefined内部形成(AngularJS - form inside ng-repeat undefined in controller)
我正在尝试访问控制器中的表单对象。 表格位于ng-repeat块内,它有动态名称,我应该能够从v1.3开始访问它。
JS:
var app = angular.module('app', []); app.controller('AppCtrl', ['$scope', function($scope) { $scope.forms = [0]; $scope.testForm = function() { if($scope.form0 == undefined) { $scope.test = 'Form is NOT defined'; } else { $scope.test = 'Form is defined'; } } }])
HTML:
<body ng-app="app"> <div ng-controller="AppCtrl"> <div ng-repeat="form in forms" ng-init="formName = 'form' + $index"> form name should be {{formName}} <form name="{{formName}}"> <input type="text" name="field" /> </form> </div> <button ng-click="testForm()">Check if form exists</button> <p ng-bind="test"></p> </div> </body>
但在我的控制器中$ scope.form0未定义 - 单击“检查表单是否存在”后,我收到“表单未定义”消息。 即使我给它一个静态名称(
<form name="form0">
),我仍然保持未定义。 当我从ng-repeat循环中取出表格时,它工作正常。知道为什么没有设置?
上面的示例: https : //plnkr.co/edit/Fvy5LdIO0H1vCS9wYR0U?p = preview
提前致谢!
I'm trying to access form object in my controller. Form lies inside ng-repeat block it has got its dynamic name and I should be able to access it since v1.3.
js:
var app = angular.module('app', []); app.controller('AppCtrl', ['$scope', function($scope) { $scope.forms = [0]; $scope.testForm = function() { if($scope.form0 == undefined) { $scope.test = 'Form is NOT defined'; } else { $scope.test = 'Form is defined'; } } }])
html:
<body ng-app="app"> <div ng-controller="AppCtrl"> <div ng-repeat="form in forms" ng-init="formName = 'form' + $index"> form name should be {{formName}} <form name="{{formName}}"> <input type="text" name="field" /> </form> </div> <button ng-click="testForm()">Check if form exists</button> <p ng-bind="test"></p> </div> </body>
But in my controller $scope.form0 is undefined - after clicking "Check if form exists" I get "Form is NOT defined" message. Even if I give it a static name (
<form name="form0">
) I still keep getting undefined. When I take form out of the ng-repeat loop, it works fine.Any idea why it is not set?
The example above: https://plnkr.co/edit/Fvy5LdIO0H1vCS9wYR0U?p=preview
Thanks in advance!
原文:https://stackoverflow.com/questions/38908056
最满意答案
很简单,你只需要添加
excluded: [':disabled'],
进入验证初始化。
例:
$('#emailForm').bootstrapValidator({ feedbackIcons: { valid: 'glyphicon glyphicon-ok', invalid: 'glyphicon glyphicon-remove', validating: 'glyphicon glyphicon-refresh' }, excluded: [':disabled'], fields: { email: { validators: { emailAddress: { message: 'The value is not a valid email address' } } } } });
It is simple, you just only need to add
excluded: [':disabled'],
Into validation init.
Example:
$('#emailForm').bootstrapValidator({ feedbackIcons: { valid: 'glyphicon glyphicon-ok', invalid: 'glyphicon glyphicon-remove', validating: 'glyphicon glyphicon-refresh' }, excluded: [':disabled'], fields: { email: { validators: { emailAddress: { message: 'The value is not a valid email address' } } } } });
相关问答
更多-
我不是百分百肯定这是你要求的但是。 Bootlint: https : //github.com/twbs/bootlint I'm not 100% sure this is what you are asking for but. Bootlint: https://github.com/twbs/bootlint
-
尝试下面的代码: $('#contactform').bootstrapValidator('resetForm', true); Try following code : $('#contactform').bootstrapValidator('resetForm', true);
-
1000hz bootstrap-validator resetForm验证结果(1000hz bootstrap-validator resetForm Validation results)[2023-12-19]
这是1000hz bootstrap-validator的一个已知问题,还没有正确的修复,可以检查这个问题Destroy方法不会删除 github上的图标 可能的解决方案是在reset或destroy表单时添加以下代码并删除error和success类。 this.$element.find('.has-error').removeClass('has-error') var $feedback = this.$element.find('.form-control-feedback') $feedbac ... -
很简单,你只需要添加 excluded: [':disabled'], 进入验证初始化。 例: $('#emailForm').bootstrapValidator({ feedbackIcons: { valid: 'glyphicon glyphicon-ok', invalid: 'glyphicon glyphicon-remove', validating: 'glyphicon glyphicon- ...
-
Jquery Validate resetForm不会清除错误消息(Jquery Validate resetForm does not clear error messages)[2022-11-08]
通常, resetForm()应该删除包含错误消息的默认标签元素。 你的代码: var formID = $(this).closest('form').attr('id'); // <- the ID of the form var form = $('#'+ formID); // <- the form object if (form.valid()){ formID.validate().resetForm(); // <- should be y ... -
\s意思是“空间”,你将它包含在角色类中,所以它是允许的。 删除它,它应该没问题。 此外,你的正则表达式可以是正义的 regexp: /^[\w]+$/ 如\w = [A-Za-z0-9_] \s Means "space" and you're including it in the character class, so it's allowing it. Remove it and it should be fine. Further, your regex can be just regexp: ...
-
我明白了。 我不得不用以下方式取消绑定表单:$('#sidebar-login')。data('validator',null); I figure it out. I had to unbind the form with:$('#sidebar-login').data('validator', null);
-
如果您仍在寻找答案,我怀疑$("form").validate()会创建一个新的验证器实例。 你需要的是以前创建的实例: $("form").data("validator").resetForm() 或者将验证器存储在变量中: var v = $("form").validate() v.resetForm() If you're still looking for the answer, I suspect that $("form").validate() creates a new valida ...
-
如果您需要重置表单并运行Validation插件方法resetForm如下所示: var v = $("form").validate({ submitHandler: function(form) { //resetForm is a method on the validation object, not the form v.resetForm(); form.reset(); } }); 这似乎解决了我在遵循您的 ...