Javascript 我无法让MEAN JS创建新客户
我是Node JS和MAEN stack的新手,我正在跟随MEANJS的教程学习它,在这个屏幕广播中,我们将创建新客户。然而,我无法创建一个新客户,因为我收到一条错误消息,上面写着“object不是一个函数”,正如谷歌chrome的控制台所建议的那样,这是指“var customer=newcustomers”这一行。这是密码Javascript 我无法让MEAN JS创建新客户,javascript,node.js,meanjs,Javascript,Node.js,Meanjs,我是Node JS和MAEN stack的新手,我正在跟随MEANJS的教程学习它,在这个屏幕广播中,我们将创建新客户。然而,我无法创建一个新客户,因为我收到一条错误消息,上面写着“object不是一个函数”,正如谷歌chrome的控制台所建议的那样,这是指“var customer=newcustomers”这一行。这是密码 customersApp.controller('CustomersCreateController', ['$scope', '$stateParams', '$loc
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;
});
};
}
]))
我真的非常感谢您的帮助,提前谢谢您在您的依赖项列表中,您正在传入$stateparms、$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 ) {
以angular声明控制器时,将传入两个参数:控制器名称和一个数组,该数组包含控制器所需的其他变量和模块的所有名称,最后是数组末尾的控制器构造函数方法
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);
}
]);
现在,构造函数方法的方法签名需要与通过数组传入的参数数组对齐
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
customersApp.controller('CustomersCreateController', ['$scope', 'Customers', '$stateParams', '$location', 'Authentication', // note that we moved 'Customers' to be next to '$scope'
function($scope, Customers ) {
如果将函数中参数的排列映射到数组中声明的内容,您将看到Customers
对象实际上映射到$stateParams
对象,并且您将其作为函数调用
如果您安排所需的参数以匹配构造函数的方法签名,那么您的示例应该开始工作(假设其他代码连接正确)
您的另一个示例之所以有效,是因为您实际上没有在代码中的任何地方使用Customers
模块
在这些控制器中,您似乎也没有使用任何其他必需的变量和模块,如果是这种情况,那么您也应该删除这些变量和模块
那么为什么会这样呢?这是因为当您通过最小化工具放置JavaScript文件时,这些工具通常会重命名传入的函数参数以减小文件的总体大小
例如。
函数(foo,bar){/*code*/}
将成为
函数(a,b){/*code*/}
因此,这为angular提供了一种方法来维护通过模块声明依赖项的方法,而不是通过最小化来删除依赖项