如何使用Angular js把一个嵌套对象放入数据库中?
How to PUT a nested object into the database using Angular js 1?
我是Angular JS 1的新手。在我的要求中,我试图使用API (PUT方法)将数据上传到mongoDB。但是我不确定如何定义一个嵌套的对象。在表单中,我有地址字段,它有两个嵌套对象permanent和postal。下面是便于理解的格式。
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>
Controller.js
(function () {
'use strict';
angular
.module('app')
.controller('userProfile.IndexController', function($scope,$http)
{
// var vm = this;
$scope.address = {
permanent {
line1: ""
zip: ""
state: ""
country: ""
},
postal {
line1: ""
zip: ""
state: ""
country: ""
}
$scope.save = function()
{
$http.put('https://student.herokuapp.com/user/personalInfo',
{name : $scope.name,
dob : $scope.dob,
gender:$scope.gender,
line1:$scope.address.permanent.line1,
zip:$scope.address.permanent.zip ,
state:$scope.address.permanent.state,
country:$scope.address.permanent.country,
line1:$scope.address.postal.line1,
zip:$scope.address.postal.zip ,
state:$scope.address.postal.state,
country:$scope.address.postal.country
}).success(function(response)
{
console.log(response);
$scope.name= "";
$scope.dob= "";
$scope.gender= "";
$scope.permanent.line1= "";
$scope.permanent.zip = "";
$scope.permanent.state= "";
$scope.permanent.country= "";
$scope.postal.line1= "";
$scope.postal.zip = "";
$scope.postal.state= "";
$scope.postal.country= "";
}
);
}
})
})();
我不太明白你的问题。看来你的$scope.address
已经按你想要的方式格式化了。
您应该能够只使用$scope.address
变量,因为它绑定到您希望PUT
的对象。
$http.put('https://student.herokuapp.com/user/personalInfo', $scope.address)
使用
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);
});
};
});
};
相关文章:
- 如何使用javascript获取嵌套对象中所有子对象的单个属性
- 如何打印嵌套对象的所有值
- 设置嵌套对象属性的更好方法
- 访问嵌套JSON对象的键,其中键是动态的
- 嵌套对象结构
- 如何递归地获取嵌套对象中所有子对象的列表
- 更改嵌套对象的父子相关id
- Node.js:用作对象嵌套元素名称的变量
- Sailsjs创建对象-嵌套创建
- Jquery $.ajax 获取响应与将“this”对象嵌套传递到“success”回调函数冲突
- 从嵌套属性数组中获取对象嵌套值
- 将对象嵌套在另一个数组中
- javascript对象嵌套回调
- 当被引用对象嵌套在ExtJS 5.0.0中时,如何读取它
- 如何在不知道其位置的情况下删除Mongo对象(嵌套的2个数组)
- 排序嵌套在对象中的数组,该对象嵌套在另一个对象中
- Javascript对象嵌套
- 在Angular中创建多维/嵌套对象(嵌套forEach循环)
- 仅列出jquery中包含json对象嵌套的所有键
- 如何将一个javascript对象嵌套在另一个对象中