Preety Sapra
Preety Sapra

Reputation: 615

How can I pass array from form using angularJs?

I have registration form and I have to pass the data in this format, till "LastName" I am able to send the data, but I am facing the issue to send the "Address" because there is one more array inside array. I tried lots of method search from google but not able to do.

{
       'Password':'125125', 
       'EmailAddress':'[email protected]',
       'FirstName':'David3',
       'LastName':'Hirst3',
       'Address':{
                  'Address1':'Unit 4',
                  'Address2':'1465 Gold Coast Hwy',
                  'State':'QLD',
                  'Suburb':'BBurleigh Heads',
                  'Postcode':'4220'
                }
    }

Please tell me how can I send this formdata with angularJs......

Upvotes: 2

Views: 1044

Answers (3)

Tim
Tim

Reputation: 2715

Try this:

{
       'Password':'125125', 
       'EmailAddress':'[email protected]',
       'FirstName':'David3',
       'LastName':'Hirst3',
       'Address':[{
                  'Address1':$scope.formData.Address1,
                  'Address2':$scope.formData.Address2,
                  'State':...,
                  'Suburb':...,
                  'Postcode':...
                }]
    }

Also I think you have a problem with your html

You should change ng-module to ng-model

From:

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

To:

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

This should work

Upvotes: 0

Preety Sapra
Preety Sapra

Reputation: 615

Here is the solution....

Here is my html code....

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 = {};
});

So, now I am getting the same format which one I want......

Upvotes: 1

underscore
underscore

Reputation: 6887

You need to have array of objects.You you need to wrap the Address object with []

  var data = {
               'Password':'125125', 
               'EmailAddress':'[email protected]',
               'FirstName':'David3',
               'LastName':'Hirst3',
               'Address':[{
                          'Address1':'Unit 4',
                          'Address2':'1465 Gold Coast Hwy',
                          'State':'QLD',
                          'Suburb':'BBurleigh Heads',
                          'Postcode':'4220'
                        }]
            };

for Address

$scope.data = data.Address;

Fiddle

Upvotes: 0

Related Questions