Javascript ng选项值等于对象,而不是值字符串
我正在构建一个小的Angular(1.5)应用程序,其中有两个链接的下拉列表。第二个根据第一个下拉菜单更改其选项。除了一个小细节,一切都很好。在HTML选项标记中,属性值等于object:[some number],而不是值本身。显示值(介于Javascript ng选项值等于对象,而不是值字符串,javascript,angularjs,json,Javascript,Angularjs,Json,我正在构建一个小的Angular(1.5)应用程序,其中有两个链接的下拉列表。第二个根据第一个下拉菜单更改其选项。除了一个小细节,一切都很好。在HTML选项标记中,属性值等于object:[some number],而不是值本身。显示值(介于和标记之间的值)显示正确。检查屏幕截图。如何使value=”“与在开始和结束标记之间显示的值相同 HTML文件: <div ng-app="App" ng-controller="app-controller"> <select id
和
标记之间的值)显示正确。检查屏幕截图。如何使value=”“
与在开始和结束标记之间显示的值相同
HTML文件:
<div ng-app="App" ng-controller="app-controller">
<select id="brand"
ng-model="input.selected_brand"
ng-options="a for (a,b) in data"
>
<option value="" disabled selected>Selection 1</option>
</select>
<select id="model"
ng-model="input.selected_model"
ng-options="a.model for a in input.selected_brand"
>
<option value="" disabled selected>Selection 2</option>
</select>
</div>
JSON文件:
{
"BMW" : [
{
"model" : "M3 CRT"
},
{
"model" : "M5"
},
{
"model" : "M3 GTS"
},
{
"model" : "M3 Coupe DCT"
},
{
"model" : "M3 F80"
},
{
"model" : "M6 Coupe"
},
{
"model" : "M5/M6 CP"
},
{
"model" : "X5/X6 M"
},
{
"model" : "M6 Gran Coupe"
},
{
"model" : "M3 E30"
}
],
"Mercedes" : [
{
"model" : "SL 65 AMG"
},
{
"model" : "600 Pullman"
},
{
"model" : "C111"
},
{
"model" : "190E 2.5-16 EII"
},
{
"model" : "SLR McLaren Stirling Moss"
},
{
"model" : "Patent-Motorwagen"
},
{
"model" : "500E"
},
{
"model" : "CLK GTR"
},
{
"model" : "300 SL"
}
],
"Lamborghini" : [
{
"model" : "Espada"
},
{
"model" : "Jalpa"
},
{
"model" : "Urraco"
},
{
"model" : "Reventon"
},
{
"model" : "Gallardo"
},
{
"model" : "Sesto Elemento"
},
{
"model" : "Diablo"
},
{
"model" : "Countach"
},
{
"model" : "Veneno"
},
{
"model" : "Miura"
}
],
"Audi" : [
{
"model" : "RS2"
},
{
"model" : "TT"
},
{
"model" : "Le Mans"
},
{
"model" : "R18 E-Tron"
},
{
"model" : "Union Type D"
},
{
"model" : "RS6 Avant"
},
{
"model" : "Horch 26/65"
},
{
"model" : "DKW Monza"
},
{
"model" : "R8"
},
{
"model" : "Quattro"
}
],
"Ford" : [
{
"model" : "Mustang Byllitt"
},
{
"model" : "Boss 429"
},
{
"model" : "SVT Cobra"
},
{
"model" : "Fairlane Thunderbolt"
},
{
"model" : "Focus RS"
},
{
"model" : "Falcon 351"
},
{
"model" : "Ford Mustang"
},
{
"model" : "Cobra R"
},
{
"model" : "Shelby GT350"
},
{
"model" : "Ford GT"
}
],
"Honda" : [
{
"model" : "S200 CR"
},
{
"model" : "Jackson Turbo"
},
{
"model" : "Accord Coupe"
},
{
"model" : "Civic Si"
},
{
"model" : "Prelude VTEC"
},
{
"model" : "Civic Mugen"
}
],
"Mazda" : [
{
"model" : "Mazda Carol"
},
{
"model" : "Rotary Pickup"
},
{
"model" : "Atenza"
},
{
"model" : "Savanna"
},
{
"model" : "RX-8"
},
{
"model" : "Eunos Cosmo"
},
{
"model" : "Autozam AZ-1"
},
{
"model" : "Cosmo L10B"
},
{
"model" : "MX-5 Miata"
},
{
"model" : "RX-7"
}
]
}
我之所以这样做是因为我
$scope.submit = function(input){
$scope.input_data = angular.copy(input);
console.log($scope.input_data);
}
需要正确的值才能正常工作,在当前状态下,console.log输出如下所示:
Object { selected_brand: Array[10], selected_model: Object }
我相信你想做的是:
<select id="brand"
ng-model="input.selected_brand"
ng-options="a as a for (a,b) in data"
>
<option value="" disabled selected>Selection 1</option>
</select>
<select id="model"
ng-model="input.selected_model"
ng-options="a.model as a.model for a in data[input.selected_brand]"
>
<option value="" disabled selected>Selection 2</option>
</select>
选择1
选择2
此外,您不必担心生成的代码中的值,因为angularjs在内部使用它来进行一些引用。真正重要的是传递给模型的实际值。angular使用内部语法设置值,以便能够正确绑定到model@charlietfl但是
$scope.input\u data=angular.copy(input)
将数组作为第一个输入值,将对象作为第二个输入值,而不是字符串值。如何解决此问题?`ng options=“a as b for(a,b)in data”>`在下拉菜单中为我提供了许多对象,而不是要选择的值。我之所以这样做,也是因为我有$scope.input\u data=angular.copy(input)
,它将第一个输入读取为数组,将第二个输入读取为对象。(我编辑了问题检查,问题的底部)。对不起。你想要什么:第一选择:汽车品牌,第二选择:汽车型号。angularjs的模型应该准确地保存什么?例如,如果您选择BMW品牌和M5模型,我需要它给我两个字符串“BMW”和“M5”。但是这段代码给了我数组和一个对象。。。您需要一个包含品牌的字符串和另一个包含型号的字符串。我要准备一个小房间是的。问题是数组1。第二个是对象,我可以很容易地从中得到字符串。但是这个数组[10]没有我可以使用的值。我不确定我在这段代码中哪里出错了,我才2天。
<select id="brand"
ng-model="input.selected_brand"
ng-options="a as a for (a,b) in data"
>
<option value="" disabled selected>Selection 1</option>
</select>
<select id="model"
ng-model="input.selected_model"
ng-options="a.model as a.model for a in data[input.selected_brand]"
>
<option value="" disabled selected>Selection 2</option>
</select>