ngOptions未在我的select元素中显示任何选项

时间:2014-07-31 17:19:29

标签: javascript html angularjs ng-options angularjs-ng-options

我的一个选择元素中的ngOptions连接到我在控制器中创建的数组,并且它没有显示任何值。我必须做些蠢事。

我确定Angular正在工作,并且控制器必须正常工作,因为我在同一个div中使用与ngModel相关的表达式(使用相同的控制器)并且它工作正常。

这是我的html的相关片段:

  

<div class="container" ng-app="tagQuestionnaireApp"> <div class="tagger" ng-controller="taggingController"> <form class="form-horizontal" name="tagForm" role="form"> <!-- Categorize Your Tag --> <div class="form-group"> <label class="sub" for="categorize">Categorize</label> <select class="form-control" id="categorize" ng-model="category" ng-options="cat.text in cat of categories"></select> </div> </form> </div> </div>

这是我的js文件:

var tagQuestionnaireApp = angular.module('tagQuestionnaireApp', []);
   tagQuestionnaireApp.controller('taggingController', ['$scope', function($scope) {
   $scope.categories = [
        {text:'Humanities', topics:[{name:"Arts and Design",color:"#ff3d00"},
                                    {name:"History",color:"#ffc107"},
                                    {name:"Linguistics",color:"#ffab00"},
                                    {name:"Literature",color:"#ff7043"},
                                    {name:"Philosophy",color:"#dd2c00"},
                                    {name:"Theology",color:"#ff6f00"},
                                    {name:"Other Humanities",color:"#ff5722"}]},
        {text:'Social Sciences', topics:[{name:"Anthropology",color:"#"},
                                    {name:"Archaeology",color:"#"},
                                    {name:"Cultural/Ethnic/Area Studies",color:"#"},
                                    {name:"Economics",color:"#"},
                                    {name:"Gender/Sexuality Studies",color:"#"},
                                    {name:"Geography",color:"#"},
                                    {name:"Political Science",color:"#"},
                                    {name:"Psychology",color:"#"},
                                    {name:"Sociology",color:"#"},
                                    {name:"Other Social Sciences",color:"#"}]},
        {text:'Natural Sciences', topics:[{name:"Space sciences",color:"#00695c"},
                                          {name:"Earth sciences",color:"#8bc34a"},
                                          {name:"Biology",color:"#64dd17"},
                                          {name:"Chemistry",color:"#00bfa5"},
                                          {name:"Physics",color:"#009688"},
                                          {name:"Material Sciences",color:"#0a7e07"},
                                          {name:"Other Natural Sciences",color:"#259b24"}]},
        {text:'Engineering', topics:[{name:"Mechanical",color:"#"},
                                     {name:"Chemical",color:"#"},
                                     {name:"Civil",color:"#"},
                                     {name:"Electrical",color:"#"},
                                     {name:"Other Engineering",color:"#"}]},
        {text:'Formal Sciences', topics:[{name:"Applied Math",color:"#738ffe"},
                                         {name:"Computer Science",color:"#7986cb"},
                                         {name:"Logic",color:"#5c6bc0"},
                                         {name:"Pure Math",color:"#4e6cef"},
                                         {name:"Statistics",color:"#3949ab"},
                                         {name:"Systems Science",color:"#283593"},
                                         {name:"Other Formal Sciences",color:"#5677fc"}]},
        {text:'Other', topics:[{name:"Other",color:"#"}]}
    ];}]);

1 个答案:

答案 0 :(得分:2)

改变这个:

<select 
  class="form-control" 
  id="categorize" 
  ng-model="category" 
  ng-options="cat.text in cat of categories"
></select>

为:

<select 
  class="form-control" 
  id="categorize" 
  ng-model="category" 
  ng-options="cat.text for cat in categories"
></select>

看看这对你是否有用。