Angularjs:错误:[ng:areq] ResevationController不是函数,未定义

时间:2015-10-15 16:41:00

标签: javascript angularjs

MainController.js

(function(){

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

    eateryControllers.controller('MainController',function(){

        var mainCtrl=this;
    });

})();

ReservationController.js

(function(){

    var eateryControllers=angular.module('eateryControllers');

    eateryControllers.controller('ReservationController',reservationController);
    function reservationController(){
        var resvnCtrl=this;
        resvnCtrl.user={};
    }

})();

我在第一个文件中创建模块并在第二个文件中重用相同的模块。

错误

http://errors.angularjs.org/1.4.7/ng/areq?p0=ReservationController&p1=not%20aNaNunction%2C%20got%20undefined
    at Error (native)
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:6:416
    at qb (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:22:131)
    at Sa (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:22:218)
    at b.$get (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:80:81)
    at g.link (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular-route.min.js:7:268)
    at aa 

2 个答案:

答案 0 :(得分:2)

你应该把你的' reservationController'一旦你在定义函数之前尝试使用函数

,它就会在你的角度控制器定义之上运行
(function(){

    var eateryControllers=angular.module('eateryControllers');
    function reservationController(){
        var resvnCtrl=this;
        resvnCtrl.user={};
    }
    eateryControllers.controller('ReservationController',reservationController);

})();

答案 1 :(得分:1)

您已将控制器定义为“ReservationController”,并在代码中将其用作“ResevationController”。错误是由于错字