当我使用的对象具有selectedOptions属性时,它不会显示在下拉列表中。但是,当我放入范围并从对象中选择时,就会显示它。任何人都知道为什么第一个不工作。这是plunker链接。选择的选项没有显示Angularjs下拉列表
http://plnkr.co/edit/XLuXmAmh4F9OobyJhBCW?p=preview
var app = angular.module('angularjs-starter', []);
var model = {
options : [
{ id: 1, name: 'foo' },
{ id: 2, name: 'bar' },
{ id: 3, name: 'blah' }],
selectedOption : { id: 1, name: 'foo' }
}
app.value('vm',model);
app.controller('MainCtrl', function($scope,vm) {
$scope.vm =vm;
$scope.items = [
{ id: 1, name: 'foo' },
{ id: 2, name: 'bar' },
{ id: 3, name: 'blah' }];
$scope.selectedItem = $scope.items[1];
});
<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>
<select data-ng-options="o.name for o in vm.options" data-ng-model="vm.selectedOption"><option value="">Select one</option></select>
<pre>{{vm.selectedOption | json}}</pre>
</body>
如何使这工作,我做了这个工作通过使用selectedoption作为ID。 plnkr.co/edit/XLuXmAmh4F9OobyJhBCW?p=preview但是每当我想显示所选的选项名称时,我都会调用一个函数来显示它。但我不喜欢这种方法。 –