【发布时间】:2016-01-23 09:27:50
【问题描述】:
我最近一直在学习 Angular,并在创建一个新网站的过程中引用了我使用教程创建的引用。按照我被告知的所有步骤,由于某种原因,我收到了这个错误。奇怪的是,它显示为某种 url。这是“错误”:
http://errors.angularjs.org/1.4.9/ng/areq?p0=PostCtrl&p1=not%20a%20function%2C%20got%20undefined
删除所有 url 乱码会使 PostCtrl 不是未定义的函数。
我不明白为什么,我查看了 Stack Overflow 的所有常见错误,例如使用全局范围、未注册控制器以及其他常见错误似乎都不是原因。这是控制器和jade文件。
控制器:
var app = angular.module("app", []);
(function() {
var PostCtrl = function ($scope, $log, $location) {
$scope.posts = [];
$scope.post = function (title, content) {
$scope.title = title;
$scope.content = content;
$log.info("Posting article: " + title + "\n" + content);
$scope.posts.push([title, content]);
};
};
app.controller("PostCtrl", ["$scope", "$log", PostCtrl]);
})();
Jade 文件:(ng-app="app") 位于布局文件中,以及所有其他涉及 angular 的脚本。
extends partials/layout
block scripts
script(src="app/controllers/PostCtrl.js")
block content
.body(ng-controller="PostCtrl")
.row
.col-xs-12
form(class="form-horizontal" ng-submit="post(title, content)")
.form-group
label(for="inputTitle" class="col-sm-2 control-label") Title
.col-sm-10
input(type="text" class="form-control" id="inputTitle" placeholder="Title" ng-model="title")
.form-group
label(for="inputContent" class="col-sm-2 control-label") Content
.col-sm-10
textarea(class="form-control" id="inputContent" placeholder="Content" ng-model="content")
.form-group
.col-sm-offset-2.col-sm-10
button(type="submit" class="btn btn-default") Post
hr
.row
.col-xs-12
【问题讨论】:
-
您在创建角度控制器时忘记了一个依赖项
app.controller("PostCtrl", ["$scope", "$log", PostCtrl]);=> 缺少"$location"。 -
有趣,那么 $location 是默认依赖吗?我一定忘记了,但不记得立即使用它。
-
你有这行
var app = angular.module("app", []);它你的其他脚本,app.js,还是其他地方? -
没有默认依赖,如果你的函数需要它,但你没有通过依赖数组注入它,它会以未定义的形式返回。
-
那么 $location 到底是用来做什么的?
标签: javascript angularjs node.js mean-stack