角度控制器未定义

时间:2016-04-07 08:17:33

标签: javascript angularjs

在我的角度项目中,我收到错误:

angular.js:13424 Error: [ng:areq] Argument 'homeController' is not a function, got undefined
http://errors.angularjs.org/1.5.3/ng/areq?p0=homeController&p1=not%20aNaNunction%2C%20got%20undefined
    at http://socket.dev/bower_components/angular/angular.js:68:12
    at assertArg (http://socket.dev/bower_components/angular/angular.js:1844:11)
    at assertArgFn (http://socket.dev/bower_components/angular/angular.js:1854:3)
    at $controller (http://socket.dev/bower_components/angular/angular.js:10003:9)
    at link (http://socket.dev/bower_components/angular-route/angular-route.js:1007:26)
    at invokeLinkFn (http://socket.dev/bower_components/angular/angular.js:9623:9)
    at nodeLinkFn (http://socket.dev/bower_components/angular/angular.js:9022:11)
    at compositeLinkFn (http://socket.dev/bower_components/angular/angular.js:8333:13)
    at publicLinkFn (http://socket.dev/bower_components/angular/angular.js:8213:30)
    at lazyCompilation (http://socket.dev/bower_components/angular/angular.js:8551:25)

的index.html

<!DOCTYPE html>
<html ng-app="pollApp">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <script src="/bower_components/angular/angular.js"></script>
    <script src="/bower_components/angular-route/angular-route.js"></script>
    <script src="/angular/app.js"></script>
    <script src="/angular/routes.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="/bower_components/bootstrap-css/js/bootstrap.min.js"></script>
    <link href="/bower_components/bootstrap-css/css/bootstrap.min.css" rel="stylesheet">
    <title>Poll</title>

</head>
<body>

<div>
    <div ng-view></div>
</div>

</body>
</html>

app.js

(function () {
    angular.module('pollApp', ['ngRoute']);
})();

homeController.js

(function()
{
    angular.module('pollApp').controller('homeController', homeController);

    function homeController()
    {

        var vm = this;
        vm.fields = 10;
        vm.creater = {};
        vm.participans = {};

        vm.test = function()
        {
            return 'gelukt!';
        }
    }
})();

这里可能有什么问题?

2 个答案:

答案 0 :(得分:1)

function homeController() {...}置于angular.module('pollApp').controller('homeController', homeController);

之上

答案 1 :(得分:0)

为什么不定义匿名函数,因为您不需要在任何地方重复使用它。

angular.module('pollApp').controller('homeController', function(){
    //controller content goes here 
});