如何在angular js中将选中的单选按钮值发送到服务器
How to send the checked radio button value to Server in angular js
我想将选中的单选按钮值发送到 angular 中的服务器
我试过了,但出现 500 服务器错误
当我只发送有效的问题 ID 时
这里是 CreateController 的代码
var CreateController = ['$scope', '$http', function ($scope, $http) {
$scope.mydata = 'ajs works';
$scope.model = {
Questions:{}
};
$http.get('/Testing/IndexVM').success(function (newdata) {
$scope.model = newdata;
});
$scope.NextQuestion = function () {
$http.post('/Testing/NextQuestion', {
quid: $scope.model.Questions.AddQuestionID, selectedopt: $scope.model.Questions.Options.OptionID
}).success(function (newdata) {
$scope.model = newdata;
});
}];
这里是索引视图的代码,我想发送
<div ng-controller="CreateController">
<tr>
<td>
<h3>Question: {{model.Questions.Question}}</h3>
</td>
</tr>
<tr ng-repeat="item in model.Questions.Options" ng-model="model.Questions.Options.OptionID">
<td class="switch radius">
<input id="{{item.Options}}" type="radio" name="selectedvalue" value="{{item.Options}}">
<label for="{{item.Options}}"></label>{{item.Options}}
</td>
</tr>
</div>
在单选按钮中添加 ng-model:
<input id="{{item.Options}}" type="radio" name="selectedvalue" value="{{item.Options}} ng-model="item">
将 ng-model 移动到 input 元素,这样每个单选按钮都具有相同的 ng-model 和相同的 name 属性,而不是你有一个单选按钮组并且模型具有所选值属性的值
所以 ng-model="model.Questions.Options.OptionID" 不是 "item"
我想将选中的单选按钮值发送到 angular 中的服务器
我试过了,但出现 500 服务器错误
当我只发送有效的问题 ID 时
这里是 CreateController 的代码
var CreateController = ['$scope', '$http', function ($scope, $http) {
$scope.mydata = 'ajs works';
$scope.model = {
Questions:{}
};
$http.get('/Testing/IndexVM').success(function (newdata) {
$scope.model = newdata;
});
$scope.NextQuestion = function () {
$http.post('/Testing/NextQuestion', {
quid: $scope.model.Questions.AddQuestionID, selectedopt: $scope.model.Questions.Options.OptionID
}).success(function (newdata) {
$scope.model = newdata;
});
}];
这里是索引视图的代码,我想发送
<div ng-controller="CreateController">
<tr>
<td>
<h3>Question: {{model.Questions.Question}}</h3>
</td>
</tr>
<tr ng-repeat="item in model.Questions.Options" ng-model="model.Questions.Options.OptionID">
<td class="switch radius">
<input id="{{item.Options}}" type="radio" name="selectedvalue" value="{{item.Options}}">
<label for="{{item.Options}}"></label>{{item.Options}}
</td>
</tr>
</div>
在单选按钮中添加 ng-model:
<input id="{{item.Options}}" type="radio" name="selectedvalue" value="{{item.Options}} ng-model="item">
将 ng-model 移动到 input 元素,这样每个单选按钮都具有相同的 ng-model 和相同的 name 属性,而不是你有一个单选按钮组并且模型具有所选值属性的值
所以 ng-model="model.Questions.Options.OptionID" 不是 "item"