[英]Drop down list and values
我在设置下拉列表的值时遇到问题。 在这里我粘贴我的代码。
HTML代码:
<form ng-submit="save()">
<table style=" table-layout: fixed;" class="table">
<thead>
<tr>
<th>Section</th>
<th>Names</th>
<th>Marks</th>
</tr>
</thead>
<tbody>
<tr>
<td>5th class</td>
<td>
<select id="5thclass" ng-model="setmarks.5thclass" ng-options="val for val in studentname">
<option ng-init="setmarks.5thclass=studentname[0]" selected="selected"></option>
</select>
</td>
<td>
<input type="number" name="5thclass" ng-model="setmarks.marks.5thclass"/>
</td>
</tr>
</tbody>
</table>
<div class="sub" class="btn-group">
<input id="savebutton" type="submit" ng-click="Submit" value="Save" class="btn btn-primary">
</div><br />
</form>
这是我的 angularjs 代码:
$scope.studentname = ["abc","pqr","xyz"];
$scope.save = function(){
$http.post('/savemarks', $scope.setmarks).success(function(data){
console.log("posted data successfully");
console.log( JSON.stringify(data));
}).error(function(data){
console.error("Error posting data");
})
}
最后这里是我的 express.js 设置。 我在哪里将表单详细信息保存到文件中。
app.post('/savemarks',urlencodedParser,function(req,res){
fs.appendFile(writecost_file,JSON.stringify(req.body,null,1),function(err){
if(err){
console.log(err);
}else{
console.log("File saved");
}
});
});
输出采用 json 格式:
{"5thclass":"abc","marks":{"5thclass":66}}
我希望输出应该是形式。
{"5thclass":"abc","marks":{"abc":66}}
在分数列中,我想要学生姓名而不是班级姓名。 谁能帮我 。 谢谢 。
您在 html 的 ng-model 指令中使用 setmarks.marks.5thclass。 因为第 5 类被视为标记对象的属性。 这就是为什么它被指定为"marks":{"5thclass":66}
。 触发保存事件后,您必须在控制器中手动创建您期望的 json 对象。 如下。 希望这会对你有所帮助。
var st = '{"5thclass" : "' + $scope.setmarks.5thclass + '","marks":{"' + $scope.setmarks.5thclass + '":' + $scope.setmarks.marks.5thclass + '} }'; var obj = JSON.parse(st);
注意:不要在 setmarks.5thclass 等属性中使用数字作为第一个字符。 这将在编译表达式时视为语法错误并抛出错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.