Ng选项不起作用

时间:2016-01-21 14:25:07

标签: angularjs ng-options angularjs-ng-options

我想使用ng-options使用AngularJS select,但它看起来不像我想的那样......

app.controller("LinkController", function($scope, $http, $filter) {
  $http.get('/api/link').success(function(data, status, headers, config) {
    $scope.pages = data.pages;
  });
});
<div ng-controller="LinkController">
  <div class="form-group">
    <label for="page" class="control-label">Pages ({{pages.length}} pages)</label>
    <select id="page" class="form-control" ng-options="page.title for page in pages">
      <option value="">-- Please, select at least one page --</option>
    </select>
  </div>
</div>

问题:我没有选项,但pages.length显示5

屏幕($ scope.pages):

enter image description here

2 个答案:

答案 0 :(得分:2)

ng-options directive requires ng-model指令与它在一起。

添加ng-model将解决问题。

答案 1 :(得分:0)

在您的选择中使用ng-model或尝试:

<div ng-controller="LinkController">
   <div class="form-group">
      <label for="page" class="control-label">Pages ({{pages.length}} pages)</label>
       <select id="page" class="form-control">
        <option value="">-- Please, select at least one page --</option>
        <option ng-repeat="page in pages">{{page.title}}</option>
       </select>
    </div>
 </div>