【问题标题】:Angular JS ng-repeat not showing optionsAngular JS ng-repeat 不显示选项
【发布时间】:2016-11-28 12:49:10
【问题描述】:

我正在尝试将数据库中的数据填充到网页中。但是,我似乎无法完成这项工作:

<div ng-controller="AnalyzerController">
   <select id="Listbox" ng-model="Listofoptions" style="width: 500px">
      <option ng-repeat="option in options" value="{{option}}"> {{option}} </option>
   </select>
   </td>
</div>

这是控制器的 javascript 代码

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"
   type="text/javascript"></script>
<script>
   var analyzer=angular.module('analyzer',[]);
   analyzer.controller('AnalyzerController',function($scope )
   {
    $scope.options = ["A","B","C","D","E"];

   }

</script>

在选择框中显示{{options}} 而不是值。

【问题讨论】:

  • 什么不起作用?您没有看到选项列表吗?
  • 为什么不使用ng-options

标签: javascript angularjs model-view-controller


【解决方案1】:

您错过了控制器函数的右括号,这就是您看不到值的原因。

AFAIK 最好将 ng-options 与 ng-model 一起使用,请参阅Choosing between ngRepeat and ngOptions 了解其好处

var analyzer=angular.module('analyzer',[]);
        
analyzer.controller('AnalyzerController', function($scope)
{
	$scope.options = ["A","B","C","D","E"];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="analyzer">
  <div ng-controller="AnalyzerController">
      <select id="Listbox" ng-model="Listofoptions" style="width: 500px">                  
        <option ng-repeat="option in options" value="{{option}}"> {{option}} </option>
      </select>
  </div>
</div>

【讨论】:

  • @SamWilson 不要忘记将 Abdul 的答案标记为正确答案。
猜你喜欢
  • 2017-04-15
  • 1970-01-01
  • 2017-09-09
  • 1970-01-01
  • 2014-11-10
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 2017-06-09
相关资源
最近更新 更多