错误:来自角度控制器的[ng:areq]

Con*_*ech 66 javascript angularjs angularjs-controller mean-stack

这是一个很长的镜头,但有没有人见过这个错误?我试图使用express,angular和mongoDB添加'Transporters'.每当我访问由传输器控制器管理的页面时,我都会收到此错误:

Error: [ng:areq] http://errors.angularjs.org/1.2.12/ng/areq?p0=TransportersController&p1=not%20aNaNunction%2C%20got%20undefined
    at Error (native)
    at http://localhost:3000/lib/angular/angular.min.js:6:450
    at tb (http://localhost:3000/lib/angular/angular.min.js:18:360)
    at Pa (http://localhost:3000/lib/angular/angular.min.js:18:447)
    at http://localhost:3000/lib/angular/angular.min.js:62:17
    at http://localhost:3000/lib/angular/angular.min.js:49:43
    at q (http://localhost:3000/lib/angular/angular.min.js:7:386)
    at H (http://localhost:3000/lib/angular/angular.min.js:48:406)
    at f (http://localhost:3000/lib/angular/angular.min.js:42:399)
    at http://localhost:3000/lib/angular/angular.min.js:42:67 
Run Code Online (Sandbox Code Playgroud)

传输器控制器如下所示:

'use strict';

angular.module('mean.transporters').controller('TransportersController', ['$scope', '$routeParams', '$location', 'Global', 'Transporters', function ($scope, $routeParams, $location, Global, Transporters) {
    $scope.global = Global;

    $scope.create = function() {
        var transporter = new Transporters({
            name: this.name,
            natl_id: this.natl_id,
            phone: this.phone
        });
        transporter.$save(function(response) {
            $location.path('transporters/' + response._id);
        });

        this.title = '';
        this.content = '';
    };

    $scope.remove = function(transporter) {
        if (transporter) {
            transporter.$remove();

            for (var i in $scope.transporters) {
                if ($scope.transporters[i] === transporter) {
                    $scope.transporters.splice(i, 1);
                }
            }
        }
        else {
            $scope.transporter.$remove();
            $location.path('transporters');
        }
    };

    $scope.update = function() {
        var transporter = $scope.transporter;
        if (!transporter.updated) {
            transporter.updated = [];
        }
        transporter.updated.push(new Date().getTime());

        transporter.$update(function() {
            $location.path('transporters/' + transporter._id);
        });
    };

    $scope.find = function() {
        Transporters.query(function(transporters) {
            $scope.transporters = transporters;
        });
    };

    $scope.findOne = function() {
        Transporters.get({
            transporterId: $routeParams.transporterId
        }, function(transporter) {
            $scope.transporter = transporter;
        });
    };
}]);
Run Code Online (Sandbox Code Playgroud)

在我的视图中,我调用列表并创建方法.它们会产生上述错误

我从ng的角度文档得到了这个:areq虽然仍然无法弄清楚发生了什么

AngularJS经常断言使用辅助函数会出现某些值并且会出现这种情况.如果断言失败,则抛出此错误.要解决此问题,请确保断言所期望的值已定义且真实.

这是调用控制器的视图public/views/transporters/list.html:

<section data-ng-controller="TransportersController" data-ng-init="find()">
    <ul class="transporters unstyled">
        <li data-ng-repeat="transporter in transporters">
            <span>{{transporter.created | date:'medium'}}</span> /
            <h2><a data-ng-href="#!/transporters/{{transporter._id}}">{{transporter.name}}</a></h2>
            <div>{{transporter.natl_id}}</div>
            <div>{{transporter.phone}}</div>
        </li>
    </ul>
    <h1 data-ng-hide="!transporters || transporters.length">No transporters yet. <br> Why don't you <a href="/#!/transporters/create">Create One</a>?</h1>
</section>
Run Code Online (Sandbox Code Playgroud)

运输商服务代码:

angular.module('transporterService', [])
    .factory('Transporter', ['$http', function($http){
        // all return promise objects
        return {
            get: function(){
                return $http.get('/api/transporters');
            },
            create: function(transporterData){
                return $http.post('/api/transporters', transporterData);
            },
            delete: function(id){
                return $http.delete('/api/transporters/'+id);
            }
        };
    }]);
Run Code Online (Sandbox Code Playgroud)

Tha*_*nga 81

我曾经历过这个错误.问题是我在两个地方用不同的参数定义了angular.module().

例如:

var MyApp = angular.module('MyApp', []);
Run Code Online (Sandbox Code Playgroud)

在其他地方,

var MyApp2 = angular.module('MyApp', ['ngAnimate']);
Run Code Online (Sandbox Code Playgroud)

  • 在整个项目代码库中,最多应该有一个地方,我们可以使用给定名称初始化app模块,即`var MyApp = angular.module('MyApp',[]);`.以及在整个代码库中可能需要的[]括号中的任何注入.在其余的文件/地方我们应该只检索app模块,即`var MyApp = angular.module('MyApp');` - 没有[]括号. (4认同)

7st*_*tud 32

我有两次错误:

1)当我写道:

var app = module('flapperNews', []);
Run Code Online (Sandbox Code Playgroud)

代替:

var app = angular.module('flapperNews', []);
Run Code Online (Sandbox Code Playgroud)

2)当我复制并粘贴一些html时,html中的控制器名称与app.js文件中的控制器名称不完全匹配,例如:

index.html的:

<script src="app.js"></script>
...
...
<body ng-app="flapperNews" ng-controller="MainCtrl">
Run Code Online (Sandbox Code Playgroud)

app.js:

var app = angular.module('flapperNews', []);

app.controller('MyCtrl', ....
Run Code Online (Sandbox Code Playgroud)

在html中,控制器名称为"MainCtrl",在js中我使用名称"MyCtrl".

错误网址中实际存在错误消息:

错误:[ng:areq] http://errors.angularjs.org/1.3.2/ng/areq?p0= MainCtrl&p1 = not%20 a%20 function%2C%20 got%20 undefined

这里没有象形文字:

MainCtrl不是一个未定义的函数

换句话说,"没有名为MainCtrl的功能.检查你的拼写."


Ant*_*zzo 25

当我在Angular控制器中定义模块但忽略了在我的HTML文件中设置应用程序名称时遇到了这个问题.例如:

<html ng-app>
Run Code Online (Sandbox Code Playgroud)

而不是正确的:

<html ng-app="myApp">
Run Code Online (Sandbox Code Playgroud)

当我定义了类似的东西:

angular.module('myApp', []).controller(...
Run Code Online (Sandbox Code Playgroud)

并在我的HTML文件中引用它.


ali*_*mat 14

你忘了在index.html中包含控制器.控制器不存在.

<script src="js/controllers/Controller.js"></script>
Run Code Online (Sandbox Code Playgroud)


Ang*_* M. 5

我有同样的错误,问题是我没有在主应用程序中注入新模块

var app = angular.module("geo", []);
...

angular
    .module('myApp', [
        'ui.router',
        'ngResource',
        'photos',
        'geo' //was missing
    ])
Run Code Online (Sandbox Code Playgroud)