如何使用angularJs从表单传递数组?

时间:2014-09-24 04:47:30

标签: arrays angularjs

我有注册表格,我必须以这种格式传递数据,直到“LastName”我能够发送数据,但我面临发送“地址”的问题,因为数组内部还有一个数组。我从谷歌尝试了很多方法搜索,但无法做到。

{
       'Password':'125125', 
       'EmailAddress':'test3@gugus.xfx.ch',
       'FirstName':'David3',
       'LastName':'Hirst3',
       'Address':{
                  'Address1':'Unit 4',
                  'Address2':'1465 Gold Coast Hwy',
                  'State':'QLD',
                  'Suburb':'BBurleigh Heads',
                  'Postcode':'4220'
                }
    }

请告诉我如何使用angularJs发送此formdata ......

3 个答案:

答案 0 :(得分:1)

这是解决方案......

这是我的HTML代码....

<强> registration.html

 <div class="list list-inset">
                <label class="item item-input item-icon-left">
                    <i class="icon ion-android-note"></i>
                    <input type="text" placeholder="Address1*" name = "Address1"  ng-module = "formdata.Address.Address1">
                </label>
                <label class="item item-input item-icon-left">
                    <i class="icon ion-android-note"></i>
                    <input type="text" placeholder="Address2*" name = "Address2" ng-module = "formdata.Address.Address1">
                </label>
                <label class="item item-input item-icon-left">
                    <i class="icon ion-android-note"></i>
                    <input type="text" placeholder="State*" name = "State" ng-module = "formdata.Address.State">
                </label>
                <label class="item item-input item-icon-left">
                    <i class="icon ion-android-note"></i>
                    <input type="text" placeholder="Suburb*" name = "Suburb" ng-module = "formdata.Address.Suburb">
                </label>
                <label class="item item-input item-icon-left">
                    <i class="icon ion-ios7-location"></i>
                    <input type="text" placeholder="Post Code*" name = "PostCode" ng-module = "formdata.Address.PostCode">
                </label>

    <div>

<强> controller.js

ctrl.controller('clinicCtrl', function($scope, $state, $window, api, $ionicLoading) {
    $scope.formData = {};
});

所以,现在我得到的格式与我想要的格式相同......

答案 1 :(得分:0)

试试这个:

{
       'Password':'125125', 
       'EmailAddress':'test3@gugus.xfx.ch',
       'FirstName':'David3',
       'LastName':'Hirst3',
       'Address':[{
                  'Address1':$scope.formData.Address1,
                  'Address2':$scope.formData.Address2,
                  'State':...,
                  'Suburb':...,
                  'Postcode':...
                }]
    }

此外,我认为您的HTML有问题

您应该将 ng-module 更改为 ng-model

自:

<input type="text" placeholder="Address1*" name = "Address1"  ng-module = "formdata.Address.Address1">

要:

<input type="text" placeholder="Address1*" name = "Address1"  ng-model = "formdata.Address.Address1">

这应该有效

答案 2 :(得分:0)

您需要拥有对象数组。您需要使用[]

包装Address对象
  var data = {
               'Password':'125125', 
               'EmailAddress':'test3@gugus.xfx.ch',
               'FirstName':'David3',
               'LastName':'Hirst3',
               'Address':[{
                          'Address1':'Unit 4',
                          'Address2':'1465 Gold Coast Hwy',
                          'State':'QLD',
                          'Suburb':'BBurleigh Heads',
                          'Postcode':'4220'
                        }]
            };

代表地址

$scope.data = data.Address;

Fiddle