我有选择框。
<select ng-model="sub_class_name" class="form-control">
<option ng-repeat="x in myData" value="{{x}}">{{x.sub_class_name}}</option>
</select>
将值传递给这样的控制器:
$scope.alertdata = function() {
var parameter = {
"first_name": $scope.first_name,
"middle_name": $scope.middle_name,
"subclass": [$scope.sub_class_name]
}
}
打印参数后
alert(JSON.stringify(parameter));
我得到字符串化的JSON输出,即。,
{"first_name":"Prashanth","middle_name":"H","last_name":"Rotti",
"subclass":["{\"sub_class_id\":3,\"sub_class_name\":\"Dependent \"}"]}
我不想要斜杠["{\"sub_class_id\":3,\"sub_class_name\":\"Dependent \"}"]
I want JSON output like this.
{"first_name":"Prashanth","middle_name":"H","last_name":"Rotti",
"subclass":[{"sub_class_id":"3","sub_class_name":"Dependent"}] }
答案 0 :(得分:0)
答案 1 :(得分:0)
添加我的评论作为答案,因为无法在评论中正确格式化数据: **更新** 由于期望的数据类型是JSON格式的字符串,因此您可以像这样初始化
$scope.sub_class_name = "{}" // Or $scope.sub_class_name ="[]"; based on data type it may be
$scope.alertdata = function() {
var parameter = {
"first_name": $scope.first_name,
"middle_name": $scope.middle_name,
"subclass": [JSON.parse($scope.sub_class_name)]
}
}
alert(JSON.stringify(parameter));
如果您遇到任何问题,请告诉我。