Javascript 使用angular js保存表单数据
我正试图保存以下表格。请检查我的html表格 html 我的结果如下Javascript 使用angular js保存表单数据,javascript,angularjs,Javascript,Angularjs,我正试图保存以下表格。请检查我的html表格 html 我的结果如下 { name:"dais", age:"24", "homenumber":"23", "road":"new road", "city":"colombo", school:"new school" } 但我需要将此表单保存为一个json 请求一个 {name:"dais", age:"24", "address":{ "homenumber":"23", "road":"new
{ name:"dais",
age:"24",
"homenumber":"23",
"road":"new road",
"city":"colombo",
school:"new school"
}
但我需要将此表单保存为一个json
请求一个
{name:"dais",
age:"24",
"address":{
"homenumber":"23",
"road":"new road",
"city":"colombo"
},
school:"new school"
}
您能帮我更改保存功能吗。您可以使用地址对象:
$scope.user = {};
$scope.user.address = {};
$scope.save = function () {
console.log($scope.user);
}
拯救
应该是
<input type="text" ng-model="user.address.homenumber"/>
<input type="text" ng-model="user.address.road"/>
<input type="text" ng-model="user.address.city"/>
您可以创建构造函数
function DataToSave(data){
this.name = data.name;
this.age = data.age;
this.address ={homenumber : data.homenumber, road:data.road, city:data.city };
this.school = data.school
}
像这样使用它
$scope.save = function (){
let dataToSave = new DataToSave($scope.user);
console.log(dataToSave);
}
<input type="text" ng-model="user.homenumber"/>
<input type="text" ng-model="user.road"/>
<input type="text" ng-model="user.city"/>
<input type="text" ng-model="user.address.homenumber"/>
<input type="text" ng-model="user.address.road"/>
<input type="text" ng-model="user.address.city"/>
function DataToSave(data){
this.name = data.name;
this.age = data.age;
this.address ={homenumber : data.homenumber, road:data.road, city:data.city };
this.school = data.school
}
$scope.save = function (){
let dataToSave = new DataToSave($scope.user);
console.log(dataToSave);
}