[英]How to bind, populate, and fetch selected value on submit of <select> with AngularJS
I have a JSON object on the scope that looks like this: 我在范围上有一个JSON对象,如下所示:
$scope.colors = [
{'colorCode': 'R', 'colorName': 'Red'},
{'colorCode': 'Y', 'colorName': 'Yellow'},
{'colorCode': 'P', 'colorName': 'Pink'},
];
I would like to bind a <select>
to this so that it looks like this 我想将<select>
绑定到此,使其看起来像这样
<select>
<option value="R">Red</option>
<option value="Y">Yellow</option>
<option value="P">Pink</option>
</select>
What I've done is this: 我所做的是:
<select
ng-model="model.selectedcolor"
ng-options="c.colorCode as c.colorName for c in colors">
</select>
However, when I inspect the html it is creating HTML like this: 但是,当我检查html时,它正在创建如下HTML:
<select>
<option value="0">Red</option>
<option value="1">Yellow</option>
<option value="2">Pink</option>
</select>
Questions 问题
value="R"...
in the HTML 如何在HTML中有value="R"...
$scope.model.selectedcolor
? 提交表单时,我可以通过$scope.model.selectedcolor
获取选定的值吗? To populate your select: 要填充您的选择:
<select ng-model="model.selectedcolor" ng-options="color.colorCode as color.colorName for color in colors" >
</select>
In your controller: 在您的控制器中:
To preselect: 预选:
$scope.model = {
selectedcolor : "Y"
}
When you submit: 提交时:
Try accessing: 尝试访问:
$scope.model.selectedcolor
It will give you Y
它会给你Y
ng-options
is arguably a better alternative than ng-repeat
-ing <option>
. 可以说ng-options
比ng-repeat
-ing <option>
更好。 This is because the ng-model
can be bound to an object (of the value as
value). 这是因为ng-model
可以绑定到对象( value as
值)。
You have specified it correctly. 您已正确指定。
Angular would, under the covers, properly map the selected <option>
value to the right object for you, and you don't even need to care about what the value
attribute of <option>
is set to - this is to answer your question #2 . 在幕后,Angular会为您正确地将选定的<option>
值映射到正确的对象,您甚至不必担心<option>
的value
属性设置为什么-这是为了回答您的问题#2 。
On question #1 , to (pre-)select an option, modify the ng-model
-bound property accordingly. 在问题#1上 ,要(预)选择一个选项,请相应地修改ng-model
-bound属性。 This will "drive" the View. 这将“驱动”视图。 In your case, set: 在您的情况下,请设置:
$scope.model.selectedcolor = "Y";
On question #3 , it depends on what you mean by "submit a form". 关于问题3 ,这取决于您“提交表单”的含义。 You typically don't classically submit a form to the server with an SPA (another reason, why you shouldn't care about that <option value="1">
for "Y"
). 通常,您通常不使用SPA向服务器提交表单(另一个原因,为什么您不关心<option value="1">
"Y"
<option value="1">
)。 $scope.model.selectedcolor
will be set to the selected colorcode
. $scope.model.selectedcolor
将设置为选定的colorcode
。
As an off-topic: 作为题外话:
You could bind your select
to the actual color
object, and not just the colorcode
property: 您可以将select
绑定到实际的color
对象,而不仅仅是colorcode
属性:
<select ng-model="model.selectedColor"
ng-options="c as c.colorName for c in colors">
</select>
Then, you can operate with the entire object. 然后,您可以对整个对象进行操作。 For example, to preselect and to submit, you would do: 例如,要预先选择并提交,您可以执行以下操作:
$scope.model.selectedColor = $scope.colors[1]; // second one is selected
$scope.submit = function(){
var colorcode = $scope.model.selectedColor.colorcode;
// post colorcode to server
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.