Javascript 如何使用AngularJS1将嵌套对象放入数据库?

Javascript 如何使用AngularJS1将嵌套对象放入数据库?,javascript,angularjs,Javascript,Angularjs,我是一个新手。在我的需求中,我试图使用API(PUT方法)将数据上传到mongoDB中。但我不确定如何定义嵌套对象。在表单中,我有地址字段,它有两个嵌套对象永久和邮政。以下是更好理解的格式 JSON数据: "address" : { "permanent" : { "line1" : "geisenheimer", "zip" : 14197, "state" : "BW", "Country" : "DE"

我是一个新手。在我的需求中,我试图使用API(PUT方法)将数据上传到mongoDB中。但我不确定如何定义嵌套对象。在表单中,我有地址字段,它有两个嵌套对象永久和邮政。以下是更好理解的格式

JSON数据:

   "address" : {
     "permanent" : {
       "line1" : "geisenheimer",
       "zip" : 14197, 
       "state" : "BW", 
       "Country" : "DE"
     },
     "postal" : {
       "line1" : "Sandwingert",
       "zip" : 69123, 
       "state" : "BW", 
       "Country" : "DE"
     }
   }
我想知道我在控制器中定义的地址是否正确

index.html

   <form class="form-horizontal">

                <div class="form-group" ng-class="{ 'has-error': form.name.$dirty && form.name.$error.required }">
                  <label for="name" class="col-sm-2 control-label" >Name</label>
                    <div class="col-sm-8">
                      <input type="text" class="form-control" id="name" ng-model="name" placeholder="Enter name as per SSLC marksheet" required>
                    </div>
                </div>
  <div class="form-group" ng-class="{ 'has-error': form.address.$dirty && form.address.$error.required }">
                  <label for="address" class="col-sm-2 control-label">Address</label>
                    <div class="col-sm-5">
                    <label style="padding-top:8px">Permanent Address</label><br>
                      <!-- <input type="text" class="form-control" id="name" placeholder="Full Name" ng-model="permanentfullname" required><br> 
                      <input type="text" class="form-control" id="name" placeholder="Address Line 1" ng-model="permanentadd1" required><br> -->
                      <input type="text" class="form-control"  placeholder="Address Line1" ng-model="address.permanent.line1" required><br>
                      <!-- <input type="text" class="form-control" id="name" placeholder="City/Town" ng-model="permanentcity" required><br> -->
                      <input type="text" class="form-control"  placeholder="State" ng-model="address.permanent.state" required><br>
                      <input type="text" class="form-control"  placeholder="Zip/Postal code" ng-model="address.permanent.zip" required><br>
                      <input type="text" class="form-control"  placeholder="Country" ng-model="address.permanent.country" required><br>
                    </div>

                    <div class="col-sm-5">
                      <label style="padding-top:8px">Postal Address</label><br>
                      <!-- <input type="text" class="form-control" id="name" placeholder="Full Name" ng-model="postalFullname" required><br> -->
                      <input type="text" class="form-control" placeholder="Address Line 1" ng-model="address.postal.line1" required><br>
                      <!-- <input type="text" class="form-control" placeholder="Address Line 2" ng-model="postaladd2" required><br> -->
                      <!-- <input type="text" class="form-control"  placeholder="City/Town" ng-model="postalcity" required><br> -->
                      <input type="text" class="form-control"  placeholder="State" ng-model="address.postal.state" required><br>
                      <input type="text" class="form-control"  placeholder="Zip/Postal code" ng-model="address.postal.zip" required><br>
                      <input type="text" class="form-control"  placeholder="Country" ng-model="address.postal.country" required><br>
                    </div>
                </div>

我不确定我是否理解这个问题。您的
$scope.address
似乎已按您所需的格式格式化

您应该能够只使用
$scope.address
变量,因为它绑定到您希望
放置的对象上

$http.put('https://student.herokuapp.com/user/personalInfo“,$scope.address)

使用此

Name : <input type="text" ng-model="name">
<button ng-click="submitFormAngular()">Calculate</button>

我正在对任何GET、POST、DELETE、PUT、patch对象使用PUT methodChaNGE方法,但它是如何嵌套的对象呢?任何对象都可以…只要data.obj=obj…尝试一下这个方法,然后在ajax之前console.log(data),因为数据的格式是正确的。在我的情况下,我如何转换它,因为它有地址,并且具有永久性和邮政性
Name : <input type="text" ng-model="name">
<button ng-click="submitFormAngular()">Calculate</button>
 var app = angular.module('formapp', []);
    app.controller('formctrl', function ($scope, $http) {
        $scope.name = 'MyName';
        $scope.submitFormAngular = function () {
            var name = $scope.name;
            var data = {};
            data.name = name;
            //var data = { 'id': 10 };
            var url = '/Home/AngularAjax';
            $http({
                method: 'PUT',
                url: url,
                data: data //forms user object
                //  headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
            }).success(function (data) {
                console.log(data);
            });


        };

    });


            };