AngularJS控制器函数-TypeError:对象不是函数
我正在创建一个电子邮件应用程序,它可以对谷歌API进行RESTful调用。我正在使用AngularJS和Java。到目前为止,我已经取得了一些成功,但我无法删除电子邮件,因为我一直收到以下错误:TypeError:object不是函数 我的角度知识有限 在我的html中,我调用函数deleteMail并传递电子邮件id 这是控制器:AngularJS控制器函数-TypeError:对象不是函数,angularjs,jakarta-ee,angularjs-scope,angularjs-controller,gmail-api,Angularjs,Jakarta Ee,Angularjs Scope,Angularjs Controller,Gmail Api,我正在创建一个电子邮件应用程序,它可以对谷歌API进行RESTful调用。我正在使用AngularJS和Java。到目前为止,我已经取得了一些成功,但我无法删除电子邮件,因为我一直收到以下错误:TypeError:object不是函数 我的角度知识有限 在我的html中,我调用函数deleteMail并传递电子邮件id 这是控制器: app.controller('InboxController', function($rootScope, $scope, $cookies, $loca
app.controller('InboxController', function($rootScope, $scope, $cookies,
$location, InboxService) {
$rootScope.loggedIn = true;
$scope.emails = InboxService.getMessages().success(function(jsonData) {
$scope.emails = jsonData;
});
$scope.deleteEmail = function(id) {
$scope.id = {
'id' : id
};
// Parse to JSON
var responseJSON = angular.toJson($scope.id);
// Make call to InboxService
var response = InboxService().del(responseJSON).success(
function(jsonData) {
response = jsonData;
if (response == 'success') {
alert('Message deleted');
} else {
alert('Message not deleted');
}
});
}
})
$scope.emails方法工作正常。给出错误的是$scope.deleteMail
服务内容如下:
app.factory('InboxService', function InboxService($http) {
var exports = {};
// Get a list of all emails
exports.getMessages = function() {
return $http.get('resources/inbox/get').error(function(data) {
console.log('There was an error!', data);
});
};
// Delete an email
exports.del = function(id) {
console.log('id ' + id);
return $http({
method : 'POST',
url : 'resources/inbox/delete',
data : id,
headers : {
'Content-Type' : 'application/json'
}
});
};
return exports;
})
不过,我觉得我的服务还不够好。问题似乎出在控制器上
控制台输出:
TypeError: object is not a function
at Scope.$scope.deleteEmail (http://localhost:8080/NewProject/js/controllers.js:64:18)
at Parser.functionCall (http://localhost:8080/NewProject/bower_components/angular/angular.js:10903:21)
at ngEventDirectives.(anonymous function).compile.element.on.callback (http://localhost:8080/NewProject/bower_components/angular/angular.js:19259:17)
at Scope.$get.Scope.$eval (http://localhost:8080/NewProject/bower_components/angular/angular.js:12811:28)
at Scope.$get.Scope.$apply (http://localhost:8080/NewProject/bower_components/angular/angular.js:12909:23)
at HTMLButtonElement.<anonymous> (http://localhost:8080/NewProject/bower_components/angular/angular.js:19264:23)
at http://localhost:8080/NewProject/bower_components/angular/angular.js:2853:10
请确保您在html中使用正确的语法调用deleteEmailid,而不使用$scope,我已经成功了。我将控制器删除方法更改为:
$scope.delete = function (id) {
InboxService.delete(id).success(function() {
$scope.loadInbox();
});
};
// Delete an email
exports.delete = function(id) {
console.log('id ' + id);
return $http({
method : 'DELETE',
url : 'resources/inbox/delete',
data: id,
headers : {
'Content-Type' : 'application/json'
}
});
}
以及本发明的服务方法:
$scope.delete = function (id) {
InboxService.delete(id).success(function() {
$scope.loadInbox();
});
};
// Delete an email
exports.delete = function(id) {
console.log('id ' + id);
return $http({
method : 'DELETE',
url : 'resources/inbox/delete',
data: id,
headers : {
'Content-Type' : 'application/json'
}
});
}
显示控制台输出。您收到的错误是什么?使用控制台输出更新可能您打算使用$http.post…我使用相同的http post格式发送电子邮件,它可以正常工作。请正确缩进和格式化代码,以便阅读和发现错误。Java与Javascript没有任何关系。删除正在传递给控制器的正确id。