0

我是 Node JS 和 MAEN 堆栈的新手,我正在关注 MEANJS 的教程来学习它,并在这个截屏视频http://www.youtube.com/watch?v=HNpMCFB8TFI&list=PL6rhBJX0L3TWYrwrQIi1_MzQDvVhkUVPI&index=26中创建新客户。但是,我无法创建新客户,因为我收到一条错误消息说“对象不是函数”,它指的是这一行“var customer = new Customers”,正如谷歌浏览器中的控制台所暗示的那样。这是代码

customersApp.controller('CustomersCreateController', ['$scope', '$stateParams', '$location', 'Authentication', 'Customers',
    function($scope, Customers ) {
            // Create new Customer
            this.create = function() {
                    // Create new Customer object
                    var customer = new Customers ({
                            firstName: this.firstName,
                            surname: this.surname,
                            suburb: this.suburb,
                            country: this.country,
                            industry: this.industry,
                            email: this.email,
                            referred: this.referred,
                            phone: this.phone,
                            channel: this.channel
                    });

                    // Redirect after save
                    customer.$save(function(response) {
                            // Clear form fields
                            $scope.firstName = '';
                            $scope.surname = '';
                            $scope.suburb = '';
                            $scope.country = '';
                            $scope.industry = '';
                            $scope.email = '';
                            $scope.referred = '';
                            $scope.phone = '';
                            $scope.channel = '';

                    }, function(errorResponse) {
                            $scope.error = errorResponse.data.message;
                    });
            };
    }      

]);

请注意,更新控制器中的更新功能可以正常工作,这是代码。

customersApp.controller('CustomersUpdateController', ['$scope', '$stateParams', '$location', 'Authentication', 'Customers',
function($scope, Customers ) {
    // Update existing Customer
    this.update = function(updatedCustomer) {
        var customer = updatedCustomer;

        customer.$update(function() {
        //wont do anything as the modal will be closed
        }, function(errorResponse) {
            $scope.error = errorResponse.data.message;
        });
    };
}

]);

我真的很感谢你的帮助,在此先感谢

4

2 回答 2

1

在您的依赖项列表中,您正在传递 $stateParams、$location 和 Authentication,这些可能不需要。

CustomersApp.controller('CustomersCreateController', ['$scope', '$stateParams', '$location', 'Authentication', 'Customers',
    function($scope, Customers ) {

无论如何,您在依赖项数组中指定的顺序是它们将被传递到您的控制器的顺序。因此,在您的控制器中,“$scope”指的是$scope,但“Customers”指的是$stateParams

您可能可以将其更改为如下所示:

CustomersApp.controller('CustomersCreateController', ['$scope', 'Customers',
    function($scope, Customers ) {

或者,如果您需要这些服务:

CustomersApp.controller('CustomersCreateController', ['$scope', '$stateParams', '$location', 'Authentication', 'Customers',
    function($scope, $stateParams, $location, Authentication, Customers ) {
于 2014-10-06T00:01:29.243 回答
1

当您以角度声明控制器时,您传入 2 个参数:控制器的名称和一个数组,其中包含控制器所需的所有其他变量和模块的名称,最后是数组末尾的控制器的构造函数方法.

现在构造方法的方法签名需要和通过数组传入的参数数组对齐。

var myApp = angular.module('myApp');
myApp.controllers('myController', ['$scope', 'foo', 'bar', // note the position for these
  function($scope, foo, bar) {                             // match the position of these
    foo.doStuff(bar);
  }
]);

因此,在您的示例中,您可以看到您错误地映射了参数

customersApp.controller('CustomersCreateController', ['$scope', '$stateParams', '$location', 'Authentication', 'Customers',
  function($scope, Customers ) { // note the arrangement of these parameters

如果您将函数中参数的排列映射到数组中声明的内容,您会看到Customers对象实际上映射到$stateParams对象并且您将其作为函数调用。

如果您安排所需的参数以匹配构造函数的方法签名,那么您的示例应该开始工作(假设其他代码连接正确)

customersApp.controller('CustomersCreateController', ['$scope', 'Customers', '$stateParams', '$location', 'Authentication', // note that we moved 'Customers' to be next to '$scope'
  function($scope, Customers ) {

您的其他示例有效的原因是您实际上并未Customers在代码中的任何位置使用该模块。

您似乎也没有在这些控制器中使用任何其他必需的变量和模块,如果是这种情况,那么您也应该删除它们。

现在至于为什么角度会这样做?这是因为当您通过最小化工具放置 JavaScript 文件时,这些工具通常会重命名函数的传入参数以减小文件的整体大小。

例如 function(foo, bar) { /* code */ }

会变成 function(a, b) { /* code */ }

因此,这为 Angular 提供了一种方法来维护一种通过模块声明依赖关系而不是通过最小化器删除它们的方法。

于 2014-10-06T00:15:57.357 回答