0

Node JS と MAEN スタックは初めてです。MEANJS のチュートリアルに従って学習しています。このスクリーンキャストhttp://www.youtube.com/watch?v=HNpMCFB8TFI&list=PL6rhBJX0L3TWYrwrQIi1_MzQDvVhkUVPI&index=26で、新しい顧客を作成します。ただし、「オブジェクトは関数ではありません」というエラーメッセージが表示されるため、新しい顧客を作成できません。これは、Google Chrome のコンソールが示唆するように、この行「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

angularでコントローラーを宣言するときは、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にはコード内のどこでもモジュールを使用していないためです。

また、これらのコントローラーで他の必要な変数とモジュールを使用しているようには見えません。その場合は、それらも削除する必要があります。

なぜAngularがこれを行うのでしょうか?これは、最小化ツールを使用して JavaScript ファイルを配置するときに、これらのツールが関数の渡されたパラメーターの名前を変更して、ファイル全体のサイズを縮小することが非常に一般的であるためです。

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

となります function(a, b) { /* code */ }

したがって、これは angular がモジュールを介して依存関係を宣言する方法を維持し、それらを最小化によって削除しないようにする方法を提供します。

于 2014-10-06T00:15:57.357 に答える