【发布时间】:2015-08-08 16:37:10
【问题描述】:
以下是我的代码,其中我没有为每个相应的行选择单选选项,让我知道我在这里做错了什么。
我的 Plnkr 代码 - http://plnkr.co/edit/MNLOxKqrlN5ccaUs5gpT?p=preview
虽然我得到了 classes 对象的名称,但没有得到选择。
HTML 代码 -
<body ng-controller="myCtrl">
<div class="container-fluid">
<form name="formValidate" ng-submit="submitForm()" novalidate="" class="form-validate form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label">Name</label>
<div class="col-sm-6">
<input type="text" name="name" required="" ng-model="classes.name" class="form-control" />
</div>
</div>
<div class="form-group">
<table id="datatable1" class="table table-striped table-hover">
<tr class="gradeA" ng-repeat="cls in reqgrps">
<td ng-bind="cls.name"></td>
<td><input type="radio" name="groupName[{{$index}}]" ng-model="classes.satisfies"> Choice 1</td>
<td><input type="radio" name="groupName[{{$index}}]" ng-model="classes.satisfies"> Choice 2</td>
<td><input type="radio" name="groupName[{{$index}}]" ng-model="classes.satisfies"> Choice 3</td>
</tr>
</table>
</div>
<div class="panel-footer text-center">
<button type="submit" class="btn btn-info">Submit</button>
</div>
</form>
</div>
<div class="result">{{classes}}</div>
</body>
脚本文件 -
var myApp = angular.module('myApp', []);
myApp.controller('myCtrl', function($scope){
$scope.reqgrps = [{name: 'Sub1', roll: 121},{name: 'Sub2', roll: 122}, {name: 'Sub3', roll: 123}];
$scope.classes = {};
$scope.result = {};
$scope.submitForm = function() {
$scope.result = $scope.classes;
};
});
------------- 编辑 -------------
预期输出 -
类 obj -
{
name: "Test Class",
satisfies: [
"Sub1": "Choice 1",
"Sub2": "Choice 3",
"Sub3": "Choice 2",
.................
..................
..................
..................
"Subn": "Choice 2",
]
}
【问题讨论】:
-
我不确定我是否理解你在这里想要做什么......你有一个空对象,你是什么,试图将被选中的单选按钮存储到那个空目的?我真的不认为你可以这样做。
-
在 ng-model 中使用不需要初始化空对象,如果它不存在,ng-model 将隐式创建 scope 属性
-
@Claies 我如何捕获为每个后续行选择的单选按钮......让我们说 Sub 1 我为 Sub 2 选择“Choice1”我选择“Choice3”......等等。 .让我知道这是否有意义
-
这是我所知道的关于单选按钮如何以角度工作的最佳描述:odetocode.com/blogs/scott/archive/2013/06/25/…
-
@Claies 我可以举个例子吗..或链接开始这个
标签: javascript angularjs html angularjs-scope