来自servicenot的回调数据返回到控制器angularjs

时间:2015-09-09 07:25:25

标签: angularjs

找到我的下面的删除函数代码,它在后端删除,并从不返回控制器的服务返回成功消息,因此无法打印咆哮消息。

这是我的控制器端代码:

$scope.deleteBlogswithid = function(id) {
                                  var loggedInUserId = $rootScope.loggedInUser.id;

                                  if ($rootScope.loggedInUser != null) {


                                    blogService.deleteBlog(id, loggedInUserId,function(data) {
                                        if(data == 'success'){

                                                $location.path("/home");
                                        $growl.box('Blog has been deleted', {  
                                            class : 'danger',
                                            sticky : false,
                                            timeout : 5000
                                        }).open();
                                        }
                                            })
                                } else {
                                    $growl.box('Please login to delete the blog', {  
                                                class : 'danger',
                                                sticky : false,
                                                timeout : 5000
                                            }).open();
                                }

                            }

service.js:

blogbeatsApp.service('blogService', function(httpService) {
    this.deleteBlog = function(id,loggedInUserId,data, callback) {
        var url = 'blog/delete/' +id + "/" + loggedInUserId;
        httpService.postRequest(url,data, callback);
    };
});

httpService.js:这是我的httpservice

blogbeatsApp.service('httpService', function($http, $location, $rootScope){
    this.postRequest = function(url, data, contentType, callback){
        $http({
            method : 'POST',
            url : url,
            data : data,
            headers : {
                'Content-Type' : contentType
            }
        }).success(function(data) {
            callback(data);
        }).error(function(data, status, headers, config) {

        });
    };

我没有在功能(数据)中获得控制器的成功消息。请帮助

1 个答案:

答案 0 :(得分:1)

正如我在评论中所说。

您没有按正确的顺序传递参数。

您的callbackpostRequest的第3个参数。哪个与function(url, data, contentType, callback) - contentType绑定。

因为,您的第三个参数在您的httpService中使用此this.postRequest = function(url, data,callback, contentType)

  

回调必须是第3个参数

更改httpSerivce

blogbeatsApp.service('httpService', function($http, $location, $rootScope){
this.postRequest = function(url, data, contentType, callback){
    $http({
        method : 'POST',
        url : url,
        data : data,
        headers : {
            'Content-Type' : contentType
        }
    }).success(function(data) {
        callback(data);
    }).error(function(data, status, headers, config) {

    });
};

致:

blogbeatsApp.service('httpService', function($http, $location, $rootScope){
this.postRequest = function(url, data, callback, contentType){
    // callback should be as 3rd parameter. 
    $http({
        method : 'POST',
        url : url,
        data : data,
        headers : {
            'Content-Type' : contentType
        }
    }).success(function(data) {
        callback(data);
    }).error(function(data, status, headers, config) {

    });
};