-1

我正在创建一个电子邮件应用程序,它可以对Google API进行REST式调用。我正在使用AngularJS和Java。到目前为止我已经取得了一些成功,但我无法删除一封电子邮件,因为我不断收到此错误:TypeError:object不是函数。AngularJS控制器函数 - TypeError:对象不是函数

我的知识是有限的。

在我的html中,我调用函数deleteEmail并传递一个电子邮件ID。

这里是控制器:

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.deleteEmail。

这里是服务:

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 
+0

显示控制台输出。你得到的错误是什么? – SoluableNonagon 2015-03-31 19:02:48

+0

更新与控制台输出 – AngularBoy 2015-03-31 19:08:39

+0

也许你打算使用'$ http.post(...' – corn3lius 2015-03-31 19:08:51

回答

0

请确保你叫从正确的语法HTML中deleteEmail(ID),而$范围

+0

将正确的ID传递给控制器​​。 – AngularBoy 2015-03-31 19:26:08

0

我得到它的工作。我改变了控制器删除方法是:

$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' 
     } 
    }); 
}