Angularjs 以各种形式动态添加表单字段 [英] Angularjs adding dynamically form fields in various forms

查看:38
本文介绍了Angularjs 以各种形式动态添加表单字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 ng-repeat 我正在创建一堆带有值的表单.对于每个表单,还有一个按钮可以将行添加到具有新字段的特定表单中.代码如下

HTML:

<h2>{{form.name}}</h2><div ng-repeat="cont in form.contacts"><input type="text" class="xdTextBox" ng-model="cont.ac"/><input type="text" class="xdTextBox" ng-model="cont.a_number"/><input type="text" class="xdTextBox" ng-model="cont.p_id"/>

<button ng-click="submit(form)">提交</button><button ng-click="addFields(form)">添加</button><小时></表单>

Javascript:

var app = angular.module('plunker', []);app.controller('MainCtrl', function($scope) {$scope.forms = [{名称":form1",ac":251,a_number":7933",p_id":33}, {名称":form2",ac":252,a_number":7933",p_id":4}, {名称":form3",ac":253,a_number":7362",p_id":3}];$scope.addFields = 函数(表单){form.contacts.push({name:'', ac: '', a_number: '', p_id: '' });}$scope.submit = 函数(表单){控制台日志(form.contacts);}});

它不起作用.这是它的plunker:http://plnkr.co/edit/THdtLgkwKrV7imqZGjL2?p=preview

这就是它应该看起来的样子(不同之处在于从 db 接收的数据对象与之前提出的问题略有不同):http://plnkr.co/edit/fETiSYVW7Y5C1yTCwizd?p=preview请让我知道问题出在哪里.谢谢

解决方案

你的 addFields 方法是问题所在.只需为 form.contacts 未定义时添加一个案例并将其设置为空数组.或者让每个表单项以一个设置为空数组的联系人键开始.

$scope.addFields = 函数(表单){if(typeof form.contacts === '未定义') {form.contacts = [];}form.contacts.push({name:'', ac: '', a_number: '', p_id: '' });}

在您的 plunk 的这个分支中处理该更改.

Angular 也有一个辅助函数,用于确定您可能想要使用的未定义内容,但我不知道它是否真的有什么不同.

angular.isUndefined(form.contacts)

Using ng-repeat I am creating bunch of forms with values in it. With each form there is also button to add rows to that particular form with new fields. Code is below

HTML:

<form name="{{form.name}}"
      ng-repeat="form in forms">          
  <h2>{{form.name}}</h2>
  <div ng-repeat="cont in form.contacts">
          <input type="text" class="xdTextBox" ng-model="cont.ac"/>
          <input type="text" class="xdTextBox" ng-model="cont.a_number"/>
          <input type="text" class="xdTextBox" ng-model="cont.p_id"/>             
  </div>
  <button ng-click="submit(form)">Submit</button>
  <button ng-click="addFields(form)">Add</button>
  <hr>
</form>

Javascript:

var app = angular.module('plunker', []);
    app.controller('MainCtrl', function($scope) {

        $scope.forms = [{
          "name" : "form1", "ac": 251, "a_number": "7933", "p_id": 33
        }, {
           "name": "form2", "ac": 252, "a_number": "7933", "p_id": 4
        }, {
           "name": "form3", "ac": 253, "a_number": "7362", "p_id": 3
        }];


        $scope.addFields = function (form) {        
            form.contacts.push({name:'', ac: '', a_number: '', p_id: '' });
        }

        $scope.submit = function(form){
          console.log(form.contacts);
        }
    });

It is not working. Here is the plunker for it: http://plnkr.co/edit/THdtLgkwKrV7imqZGjL2?p=preview

This is how it should be looking(Difference is data object received from db is little different than this previously asked question): http://plnkr.co/edit/fETiSYVW7Y5C1yTCwizd?p=preview Please let me know where the problem is. Thanks

解决方案

Your addFields method is the problem. Just add a case for when form.contacts is undefined and set it to empty array. Or make each form item start with a contacts key set to an empty array.

$scope.addFields = function (form) {
  if(typeof form.contacts === 'undefined') {
    form.contacts = [];
  }
  form.contacts.push({name:'', ac: '', a_number: '', p_id: '' });
}

Works with that change in this fork of your plunk.

Angular also has a helper function for determining when something is undefined you might want to use though I do not know if it really makes any difference.

angular.isUndefined(form.contacts)

这篇关于Angularjs 以各种形式动态添加表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆