我得到一个不是aNaNunction错误,无法找出原因

时间:2016-11-14 23:44:47

标签: angularjs

我是angularjs的新手,并搜索了stackoverflow并尝试了许多修复程序。我已将控制器包含在index.html中。这是html代码:

    <div data-ng-app="expertise-choice" data-ng-controller="expertiseController">
    <label>
      Select your area of expertise or interest
      <select ng-model="expertise" ng-options="x for x in expertise"></select>
    </label>
  </div>

我的控制器代码是:

    var app = angular.module('expertise-choice' []);
    app.controller('expertiseController', function($scope) {
      $scope.expertise = ["African-American Studies", "Gender Studies", "Hispanic Studies", "More to come"];
    });

我删除了angular.module中的[]。我已经尝试将put()放在代码的末尾,如})();我看不到任何错误/错别字。请帮帮我!

2 个答案:

答案 0 :(得分:1)

您在ng-modelng-options中使用了同一个对象。当Angular尝试绑定模型时,它将覆盖您的选项。

解决方案:使用其他型号

<select ng-model="selectedExpertise"
        ng-options="x for x in expertise">
</select>

...演示

&#13;
&#13;
angular.module('expertise-choice', [])
  .controller('expertiseController', function($scope) {
    $scope.expertise = ["African-American Studies", "Gender Studies", "Hispanic Studies", "More to come"];
  });
&#13;
<div data-ng-app="expertise-choice" data-ng-controller="expertiseController">
  <label>
    Select your area of expertise or interest
    <select ng-model="selectedExpertise"
            ng-options="x for x in expertise"></select>
  </label>
  <pre>selectedExpertise = {{selectedExpertise | json}}</pre>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您在控制器代码中遗漏了一个逗号,声明了您的模块:

var app = angular.module('expertise-choice', []);
app.controller('expertiseController', function($scope) {
    $scope.expertise = ["African-American Studies", "Gender Studies", "Hispanic Studies", "More to come"];
});