我正在使用yeoman的angular-fullstack生成器制作一个小型民意调查应用程序。我为民意调查设置了一些虚拟数据。当用户点击一个投票时,他们会被路由到一个查看页面,在那里他们可以根据为问题设置的答案输入他们的选择。基于用户选择获取然后发布
当前,当用户选择一个选项并按下提交时,更改会在客户端更新,但我无法知道要为数据库POST请求放置什么。
这里是view
观点:
<div class="container">
<h3>{{poll.title}}</h3>
<form ng-submit="submitForm()">
<div ng-repeat="answer in poll.answers">
<label><input type="radio" name="option" ng-model="radioData.index" value="{{$index}}"/>
{{ answer.value }} - {{ answer.votes }} Votes
</label>
</div>
<button class="btn btn-success" type="submit">Vote!</button>
</form>
</div>
这里是它的控制器:
'use strict';
angular.module('angFullstackCssApp')
.controller('ViewCtrl', function ($scope, $routeParams, $http) {
$http.get('/api/polls/' + $routeParams._id).success(function (poll) {
console.log(poll);
$scope.poll = poll;
$scope.radioData = {
index: 0
};
$scope.viewPoll = true;
$scope.alreadyVoted = false;
$scope.submitForm = function() {
$scope.alreadyVoted = true;
console.log($scope.radioData.index);
$scope.poll.answers[$scope.radioData.index].votes += 1;
// Change database entry here
};
});
});
这里是我的调查模式:
var PollSchema = new Schema({
creator: String,
title: String,
answers: [{
value: String,
votes: Number
}]
});
所以我的问题是: -
如何编写合适的POST请求以递增投票答案?
在我这样做的GET请求中编写我的代码是否正确?感觉好像有一个更合适的方法来做到这一点,特别是由于我正在调用另一个请求。