2016-04-07 45 views
1

在我的角度項目中,我收到錯誤:角控制器沒有定義

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!'; 
     } 
    } 
})(); 

這裏有什麼問題?

+4

您的索引中沒有'homeController.js' –

回答

1

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

0

爲什麼不直接定義一個匿名函數,你將不需要任何地方重新使用。

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