gpt4 book ai didi

javascript - 参数不是函数 - AngularJS 未定义

转载 作者:行者123 更新时间:2023-12-02 14:50:44 25 4
gpt4 key购买 nike

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

我在 Webstorm 中有一个使用 Express 和 npm 的小项目。 Angular 作为依赖项添加,版本 1.5.3。我也使用 Jade 作为模板语言。这是我在 Plunker 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)

我的app.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' });
}]);

我的controllers.js

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

还有我的 services.js:

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

我得到的错误是:

angular.js:13294 Error: [ng:areq] Argument 'CoursesListController' is not a function, got undefined

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

最佳答案

这是一个加载顺序问题。

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

要解决此问题,只需将 script(src='/javascripts/app.js') 移至其他包含项下方即可。

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

关于javascript - 参数不是函数 - AngularJS 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36203149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com