首页 \ 问答 \ AngularJS - 在控制器中的ng-repeat undefined内部形成(AngularJS - form inside ng-repeat undefined in controller)

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
更新时间:2022-03-10 22:03

最满意答案

很简单,你只需要添加

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的一个已知问题,还没有正确的修复,可以检查这个问题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- ...
  • 通常, 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(); } }); 这似乎解决了我在遵循您的 ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。