Angular JS无法注册控制器

时间:2014-06-11 06:24:00

标签: angularjs

我有一个非常简单的Angular JS应用程序,我无法弄清楚为什么我收到错误:错误:[ng:areq]参数'CoursesController'不是函数,未定义

我的小提琴(您需要打开浏览器开发工具以查看控制台中的错误):http://jsfiddle.net/Y4c2q/

我的Angular JS代码:

'use strict';
console.log('reg module entry');
var registrationModule = angular.module('registrationModule', []);

registrationModule.controller('CoursesController', function ($scope, bootStrappedData) {
    $scope.courses = bootStrappedData.courses;
    console.log('inside of controller');
});

registrationModule.factory('bootStrappedData', function() {
    console.log('bootstrapping our data');
    return {
courses: [{"number":"aaa","name":"magical creatures","instructor":"John"},
          {"number":"bbb","name":"ze dark arts","instructor":"Rob"},
          {"number":"ccc","name":"third book","instructor":"Bran"}]
     };
});

这是我要显示3行数据的HTML:

<div ng-controller="CoursesController">
    <table class="table table-condensed table-hover">
        <tr>
            <th>Course</th>
            <th>Course Name</th>
            <th>Instructor</th>
        </tr>
        <tr ng-repeat="course in courses">
            <td>{{course.number}}</td>
            <td>{{course.name}}</td>
            <td>{{course.instructor}}</td>
        </tr>
    </table>
</div>

1 个答案:

答案 0 :(得分:4)

此错误即将发生,因为您的ng-app声明缺少模块名称。使用此

ng-app='registrationModule'

这会将您的模块和控制器链接到视图。

相关问题