简体   繁体   English

如何在提交以下内容时绑定,填充和获取所选值 <select>使用AngularJS

[英]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 问题

  1. How can I pre-select the second item in the drop down (in this case, Yellow) 如何预选下拉菜单中的第二项(在这种情况下为黄色)
  2. How can I have value="R"... in the HTML 如何在HTML中有value="R"...
  3. When I submit the form, will I be able to get the selected value by $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

DEMO DEMO

DEMO WITH SUBMIT 演示与提交

ng-options is arguably a better alternative than ng-repeat -ing <option> . 可以说ng-optionsng-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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM