我正在尝试让我的第一个真正的 MEAN 堆栈工作,所以这可能是一个新手错误。
为什么应用程序中的角度模块.js和控制器中的函数.js仍然未定义(未定义"Angular","未定义PollListCtrl"等)
提前非常感谢!
这是索引.翡翠
extends layout
doctype html
html(lang='en', ng-app='polls')
block content
p Welcome to #{title}
head
meta(charset='utf-8')
meta(name='viewport', content='width=device-width, initial-scale=1, user-scalable=no')
title= title
script(src='https://ajax.googleapis.com/ajax/libs/angularjs/1.2.27/angular.js')
script(src='//ajax.googleapis.com/ajax/libs/angularjs/1.2.27/angular-resource.js')
script(src='/public/js/controllers.js')
link(rel='stylesheet', href='//netdna.bootstrapcdn.com/bootstrap/3.0.1/css/bootstrap.css')
link(rel='stylesheet', href='/stylesheets/style.css')
script(src='/public/js/services.js')
script(src='/public/js/app.js')
body(ng-controller='PollListCtrl')
nav.navbar.navbar-inverse.navbar-fixed-top(role='navigation')
div.navbar-header
a.navbar-brand(href='#/poll')= title
div.container
div(ng-view)
和控制器的第一行.js
function PollListCtrl($scope, Poll) {
$scope.polls = Poll.query();
}
。和应用程序.js
angular.module('polls', ['pollServices']).config(['$routeProvider', function($routeProvider) {
$routeProvider.when('/polls', { templateUrl: 'partials/list.html', controller:
PollListCtrl }).when('/poll/:pollId', { templateUrl: 'partials/item.html', controller:
PollItemCtrl }). when('/new', { templateUrl: 'partials/new.html', controller:
PollNewCtrl }).otherwise({ redirectTo: '/polls' });
}]);
您这里有一些需要清理的问题。
- 首先,您需要引用您的应用程序.js在 angular 之后.js但在任何其他可能使用它的模块之前。
- 其次,应用的签名设置不正确。 您需要分配
angular.module
到一个变量,即:var polls = angular.module('polls', ...
. - 您的控制器签名也不正确。 为了使控制器能够访问应用的资源,需要在应用模块上定义它。
polls.controller('pollListCtrl', [$scope, function($scope) { ...
另请注意,控制器名称通常不以大写字母开头。
希望这能让你更成熟一些,但我强烈建议你阅读 Angular.js 教程,这样你对有棱角的做事方式有更好的感觉。
编辑
如前所述,作为替代方法,您可以跳过将应用程序分配给变量,然后在挂接控制器时完全引用该应用程序:
angular.module('polls').controller('pollListCtrl', [$scope, function($scope){
...
大多数示例将显示第一种形式,但许多人认为更明确的设计是更好的做法。