这是AngularJS的问题;我有一个简单的表单:
<input type="text" class="form-control" id="qus" placeholder="Enter Question" ng-model="qus">
<input type="text" class="form-control" id="op1" placeholder="Option 1" ng-model="op1">
<label><input type="checkbox" ng-model="correct1">Correct</label>
<button class="form-control btn btn-primary" ng-click = "save()">Save</button>
<pre ng-bind="dataShow"></pre>
脚本:
var app = angular.module('qApp', []);
app.controller('qCtrl', function($scope) {
var set = [];
var op1 = [];
$scope.save = function (){
if($scope.correct1!==true){$scope.correct1=false;}
op1.push($scope.op1, $scope.correct1);
var qus = [$scope.qus, op1];
set.push(qus);
$scope.dataShow = JSON.stringify(set);
};
});
输出:
使用JSON.stringify
作为第一个条目后,输出如下所示:
[
["q1",["o1",false]]
]
,第二个条目的输出是:
[
["q1", ["o1",false,"o2",true]],
["q2", ["o1",false,"o2",true]]
]
但是我想要这样的东西:
[
["q1", ["o1",false,]],
["q2", ["o2",true]]
]
怎么做?"true
/false
"为复选框的值
@Nagesh Sanika是正确的。每个问题都有一个选项列表。因此,每次向集合中添加新问题时,都可以创建选项数组。
var app = angular.module('qApp', []);
app.controller('qCtrl', function($scope) {
var set = [];
$scope.save = function (){
var op1 = []; // Moved it inside the save method
if($scope.correct1!==true){$scope.correct1=false;}
op1.push($scope.op1, $scope.correct1);
var qus = [$scope.qus, op1];
set.push(qus);
$scope.dataShow = JSON.stringify(set);
};
});
这是活塞。
要获得结果,需要每次清空op1数组。
var app = angular.module('qApp', []);
app.controller('qCtrl', function($scope) {
var set = [];
var op1 = [];
$scope.save = function (){
if($scope.correct1!==true){
$scope.correct1=false;
}
op1.push($scope.op1, $scope.correct1);
var qus = [$scope.qus, op1];
set.push(qus);
op1=[];
$scope.dataShow = JSON.stringify(set);
};
});
希望这行得通