简体   繁体   English

如何在AngularJS中的选择(HTML)列表中填充值

[英]How to populate a value in a select ( HTML ) list in AngularJS

I am using a select list in a particular edit form page. 我在特定的编辑表单页面中使用选择列表。 Whenever a particular entry has to be edited, a new state with the edit form appears. 每当必须编辑特定条目时,就会出现带有编辑表单的新状态。 It has a select list which has to populated with one of its options (ideally the value that is fetched from the server using API has to be populated into the select). 它具有一个选择列表,该列表必须填充其选项之一(理想情况下,必须将使用API​​从服务器获取的值填充到选择列表中)。

I am using Angular JS on the client side. 我在客户端使用Angular JS。 Could somebody please tell me how to achieve the same thing using Angular JS? 有人可以告诉我如何使用Angular JS实现相同的功能吗?

I need that initial value inserted by the user in the select to be shown up as a default when the edit page is opened. 我需要用户在选择中插入的初始值在打开编辑页面时显示为默认值。

(MOREOVER, MY PLACEHOLDER NOT WORKING:"Choose a country") (此外,我的代理人无法使用:“选择国家/地区”)

Lets say there is something like this: 可以说是这样的:

 <div class="col-md-8"> <select data-placeholder="Choose a Country..." class="chosen-select form-control" style="width:350px;" tabindex="2" required="" ng-model="location"> <option value="" disabled selected>Select</option> <option ng-repeat="location in locations1" value="{{location.id}}">{{location.name}}</option> </select> </div> 

使用ngOptions

<select ng-options="location as location.name for location in locations track by location.id" ng-model="selected"></select>

With ng-options you can do it like this: 使用ng-options可以这样:

<div class="col-md-8">
<select ng-model="selectedLocation" ng-options="location.id as location.name for location in locations1" class="chosen-select form-control" style="width:350px;" tabindex="2">
    <option value="" disabled selected>Choose a Country</option>
</select>
</div>

The location.id is set to the options value and the location.name is set to the text. location.id设置为选项值,而location.name设置为文本。

To use a placeholder in a select is kind of what the default options does for you. 在选择中使用占位符是默认选项对您的作用。 So I would set the default option text to "Choose a country". 因此,我将默认选项文本设置为“选择国家”。 Se this SO answer on that topic 这个问题的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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