角度控制器不是函数,未定义

时间:2016-01-22 21:16:39

标签: javascript angularjs node.js mean-stack

我最近一直在学习Angular,并且正在创建一个我用教程创建的新网站。按照我被告知的所有步骤,出于某种原因我收到了这个错误。奇怪的是,它显示为某种网址。这是"错误":

http://errors.angularjs.org/1.4.9/ng/areq?p0=PostCtrl&p1=not%20a%20function%2C%20got%20undefined

删除所有url乱码叶子PostCtrl不是一个函数未定义。

我不明白为什么,我已经查看了Stack Overflow和所有常见错误,例如使用全局范围,没有注册控制器,以及其他常见错误似乎不是原因。这是控制器和玉器文件。

控制器:

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")在布局文件中,以及涉及角度的所有其他脚本。

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

2 个答案:

答案 0 :(得分:1)

@FlorianTopf救了一天。我没有将$ location作为依赖项。新的PostCtrl控制器:

(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", "$location", PostCtrl]);

})();

答案 1 :(得分:0)

您正在启动两次控制器。从html .body(ng-controller="PostCtrl")

中删除它