未定义“角度”(脚本未链接在一起)
'angular' is not defined (scripts not linking together)
我正在尝试让我的第一个真正的 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){
...
大多数示例将显示第一种形式,但许多人认为更明确的设计是更好的做法。
相关文章:
- JavaScript链接在点击时不起作用;函数在页面加载时工作
- 在 JavaScript 中使用侦听器将输入字段链接在一起
- 我想通过单击按钮将 2 个 php 文件链接在一起
- 如何将(CSS/HTML)的包链接在一起.CSS/HTML->CSS/HTML->CSS/HTML
- 我可以将javascript和方法中的属性链接在一起吗?
- 未定义“角度”(脚本未链接在一起)
- 使用一行将标记链接在一起:googlemapsapi
- 将承诺与多个参数链接在一起
- 如何在react中将单独文件中的组件链接在一起
- JavaScript变量毫无理由地链接在一起
- 如何将CSS、javascript和HTML文件链接在一起
- 如何使用jquery将两个元素关联/链接在一起
- 将大量(>40,000)原生承诺链接在一起会消耗太多内存
- 如何将这些函数与承诺链接在一起
- javascript数组中的对象神秘地链接在一起
- jQuery-如何将DOM元素和Javascript对象链接在一起
- AngularJS-将两个指令绑定/链接在一起
- 使用Jquery(甚至只是Javascript),了解如何将命令链接在一起
- 拥有多个日期选择器并将它们全部链接在一起
- 将多个Select2链接在一起