我在其他帖子上读到过,但我想不出来.

我有一个数组,

$scope.items = [
   {ID: '000001', Title: 'Chicago'},
   {ID: '000002', Title: 'New York'},
   {ID: '000003', Title: 'Washington'},
];

我希望将其呈现为:

<select>
  <option value="000001">Chicago</option>
  <option value="000002">New York</option>
  <option value="000003">Washington</option>
</select>

我还想 Select ID=000002的选项.

我读过100,也试过了,但我想不出来.

推荐答案

需要注意的一点是,ngModel是required,用于ngOptions的...注意ng-model="blah",它表示"将$scope.blah设置为所选值".

试试这个:

<select ng-model="blah" ng-options="item.ID as item.Title for item in items"></select>

以下是AngularJS文档中的更多内容(如果您没有看到):

对于数组数据源:

  • 数组中值的标签
  • select as 数组中值的标签
  • 数组中值的GROUP BY GROUP标签 =为数组中的值 Select 作为标签GROUP BY GROUP

对于对象数据源:

  • 对象中(键、值)的标签
  • select as 对象中(键、值)的标签
  • 对象中按GROUP FOR(关键点、值)分组的标签
  • select as 对象中按GROUP FOR(关键点、值)分组的标签

有关AngularJS中选项标记值的一些说明:

当你用ng-options,the values of option tags written out by ng-options will always be the index of the array item the option tag relates to的时候.这是因为AngularJS实际上允许您使用 Select 控件 Select 整个对象,而不仅仅是基本类型.例如:

app.controller('MainCtrl', function($scope) {
   $scope.items = [
     { id: 1, name: 'foo' },
     { id: 2, name: 'bar' },
     { id: 3, name: 'blah' }
   ];
});
<div ng-controller="MainCtrl">
   <select ng-model="selectedItem" ng-options="item as item.name for item in items"></select>
   <pre>{{selectedItem | json}}</pre>
</div>

以上将允许您直接 Select $scope.selectedItem中的整个对象.The point is, with AngularJS, you don't need to worry about what's in your option tag. Let AngularJS handle that; you should only care about what's in your model in your scope.

Here is a plunker demonstrating the behavior above, and showing the HTML written out


处理默认选项:

关于缺省选项,有几件事我在上面没有提到.

Selecting the first option and removing the empty option:

您可以通过向ng-options中重复的项目中的第一个元素添加一个简单的ng-init来设置模型(从ng-model开始)来实现此目的:

<select ng-init="foo = foo || items[0]" ng-model="foo" ng-options="item as item.name for item in items"></select>

注意:如果foo恰好被正确初始化为"falsy",这可能会有点疯狂.在这种情况下,很可能需要在控制器中处理foo的初始化.

Customizing the default option:

这有点不同;这里您需要做的就是添加一个选项标记作为SELECT的子项,该标记具有空值属性,然后定制其内部文本:

<select ng-model="foo" ng-options="item as item.name for item in items">
   <option value="">Nothing selected</option>
</select>

注意:在这种情况下,即使 Select 了其他选项,"empty"选项仍将保留.AngularJS下的selects的默认行为并非如此.

A customized default option that hides after a selection is made:

如果希望自定义默认选项在 Select 值后消失,可以向默认选项添加ng hide属性:

<select ng-model="foo" ng-options="item as item.name for item in items">
   <option value="" ng-if="foo">Select something to remove me.</option>
</select>

Javascript相关问答推荐

详细说明如何以图表方式改变仪表的范围和 colored颜色

Express.js:以块形式发送响应

功能和普通对象之间的原型污染

如何在JavaScript中通过一次单击即可举办多个活动

如何从JSON数组添加Google Maps标记—或者如何为数组添加参数到标记构造器

如何在Connect 4游戏中 for each 玩家使用位板寻找7形状?

在Vite React库中添加子模块路径

配置WebAssembly/Emscripten本地生成问题

将旋转的矩形zoom 到覆盖它所需的最小尺寸&S容器

WebGL 2.0无符号整数输入变量

使用ThreeJ渲染的形状具有抖动/模糊的边缘

使用POST请求时,Req.Body为空

IF语句的计算结果与实际情况相反

在使用位板时,如何在Java脚本中判断Connect 4板中中柱的对称性?

TypeError:无法读取未定义的属性(正在读取';宽度';)

当代码另有说明时,随机放置的圆圈有时会从画布上消失

使用父标签中的Find函数查找元素

为什么延迟在我的laravel项目中不起作用?

用于部分字符串的JavaScript数组搜索

判断函数参数的类型