错误:Argumentcontroller不是函数,未定义

时间:2014-04-16 04:53:23

标签: javascript angularjs

我正在使用Angular来解决问题并且让我的沙盒应用程序运行时出现问题。我想使用服务实现一个简单的控制器。但是,我收到以下错误:

  

错误:参数'UserController'不是函数,未定义

按所列顺序在页面上引用文件。 webConfig.JS

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

UserController.JS

app.controller('UserController', function ($scope, UserService) {
    // Define the model properties. The view will loop
    // through the services array and genreate a li
    // element for every one of its items.

    $scope.services = [
        {
            name: 'Web Development',
            price: 300,
            active: true
        }, {
            name: 'Design',
            price: 400,
            active: false
        }, {
            name: 'Integration',
            price: 250,
            active: false
        }, {
            name: 'Training',
            price: 220,
            active: false
        }
    ];

    $scope.LogInfo = {
        UserName: 'username',
        Password: 'password'
    };

    $scope.ProcessLogin = function (Info) {
        UserService.GetLoginStatus(Info);
    };

    $scope.toggleActive = function (s) {
        s.active = !s.active;
    };

    // Helper method for calculating the total price

    $scope.total = function () {

        var total = 0;

        // Use the angular forEach helper method to
        // loop through the services array:

        angular.forEach($scope.services, function (s) {
            if (s.active) {
                total += s.price;
            }
        });

        return total;
    };
});

UserService.JS

app.service('UserService', function () {
    this.GetLoginStatus = function (Info) { 
        alert(JSON.stringify(Info)) 
    };
});

2 个答案:

答案 0 :(得分:0)

因为您的应用程序跨越多个文件,我不会通过app变量将控制器添加到您的模块,而是通过其名称查找模块...

变化:

app.controller('UserController', function ($scope, UserService) {
&
app.service('UserService', function () {

要:

angular.module('app').controller('UserController', function ($scope, UserService) {
 &
angular.module('app').service('UserService', function () {

答案 1 :(得分:0)

我明白了。问题实际上源于我的HTML。 一旦我添加了ng-app =" app"我在哪里引用控制器然后一切正常。

<body ng-controller="UserController" ng-app="app">