默认情况下选择一个选项

时间:2016-02-23 06:10:07

标签: angularjs angularjs-ng-model

我正在努力使用以下代码。

  <body ng-controller="MainCtrl">
    <h1>Select something below</h1>
    <select id="s1" ng-model="selectedItem" ng-options="item as item.name for item in items"></select>
    <h3>The selected item:</h3>
    <pre>{{selectedItem | json}}</pre>
    <h3>The inner html of the select:</h3>
    <pre id="options" class="prettify html"></pre>
  </body>

和js

var app = angular.module('angularjs-starter', []);

app.controller('MainCtrl', function($scope) {
  $scope.items = [
    { id: 1, name: 'foo' },
    { id: 2, name: 'bar' },
    { id: 3, name: 'blah' }];

  $scope.selectedItem = { id: 3, name: 'blah' };

});

我默认选择了一个项目。但是在页面加载时它并没有显示在选择中。

我在以下链接中提供了我的代码

http://plnkr.co/edit/FlESsL?p=preview

在给定示例中,默认情况下,下拉列表应选择项blah

TIA 莫宾

3 个答案:

答案 0 :(得分:1)

您需要做的第一件事是更新您的angularjs版本,然后您可以简单地按照角度select示例。

在提供的链接中,他们使用track by来显示正确的对象,我冒昧地编辑了他们的示例plunker并添加了代码Here it is!

正如您所看到的,我在代码中添加的唯一内容是track by item.id

答案 1 :(得分:0)

试试这个:

$pdf->Cell(30, 0, 'Bottom-Bottom', 1, $ln=0, 'C', 0, '', 0, false, 'B', 'B');

<select id="s1" ng-model="selectedItem" ng-options="item.id as item.name for item in items"></select>

答案 2 :(得分:0)

在提出问题之前,你经历过 - https://docs.angularjs.org/api/ng/directive/ngOptions

只需在控制器中进行以下更改 -

$ scope.selectedItem = $ scope.items [0];

相关问题