我有選擇框。如何使用Angular JS在選擇框中選擇包括id和名稱值在內的所有字段
<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"}] }
您是否嘗試過使用JSON.parse 「繼承」:JSON.parse($ scope.sub_class_name) – undefined
使用[NG-選項](https://docs.angularjs.org/api/ng/directive/ngOptions),而不是爲了選擇整個對象。現在您正在選擇一個字符串的選項的值。 – andreim