参数不是函数-AngularJS未定义

Argument is not a function - AngularJS undefined

本文关键字:-AngularJS 未定义 函数 参数      更新时间:2023-09-26

我是Angular的新手,目前我正在尝试构建一些非常简单的东西,但我遇到了一个问题,我被卡住了,我不明白为什么会发生这种情况。

我在Webstorm中有一个关于Express和npm的小项目。Angular被添加为一个依赖项,版本1.5.3。我也使用Jade作为模板语言。这是我在普朗克的东西http://plnkr.co/edit/qCR420hBgnlcUisSOwgw?p=preview

还列出如下:

doctype html
html(lang='en', ng-app='courses')
head
    meta(charset='utf-8')
    meta(name='viewport', content='width=device-width, initial-scale=1, user-scalable=no')
    script(src='/node_modules/angular/angular.js')
    script(src='/node_modules/angular-resource/angular-resource.js')
    script(src='/node_modules/angular-route/angular-route.js')
    script(src='/javascripts/app.js')
    script(src='/javascripts/controllers.js')
    script(src='/javascripts/services.js')
    title= title
    link(rel='stylesheet', ref='//netdna.bootstrapcdn.com/bootstrap/3.0.1/css/bootstrap.min.css')
    link(rel='stylesheet', href='/stylesheets/style.css')
body(ng-controller='CoursesListController')
    nav.navbar.navbar-inverse.navbar-fixed-top(role='navigation')
        div.navbar-header
            a.navbar-brand(href='#/courses')=title
    div.container
        div(ng-view)

我的应用程序js:

angular.module('courses', ['ngRoute'])
.config(['$routeProvider', function ($routeProvider) {
    $routeProvider
        .when('/courses', { templateUrl: 'partials/list.html', controller: CoursesListController })
        .when('/course/:courseId', { templateUrl: 'partials/item.html', controller: CourseItemController })
        .when('/new', { templateUrl: 'partials/new.html', controller: CourseCreateController })
        .otherwise({ redirectTo: '/courses' });
}]);

我的控制器.js

function CoursesListController($scope, $routeParams, Course) {
    $scope.courses = Courses.query();
}

还有我的服务js:

angular.module('courseServices', ['ngResource']).factory('Course', function 
($resource) {
    return $resource('courses/:courseId', {}, {
        query: {method: 'GET', params: {courseId: 'courses'}, isArray: true}
    })
});

我得到的错误是:

angular.js:13294错误:[ng:areq]参数"CoursesListController"不是函数,得到了未定义的

我不知道为什么。此外,也许一些技巧会很有用——你认为这样的结构/分离有意义吗?

这是一个装载订单问题。

基本上,您首先加载app.js,它试图注册尚未加载的函数。

要解决此问题,只需将script(src='/javascripts/app.js')移动到其他includes下面即可。

编辑:另外,还有一大堆替代方案可以防止此类问题,如RequireJS、Webpack、Grunt等。