[英]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.