2015-09-09 40 views
1

找到我的下面的代码删除函数它的后端删除,并返回成功的消息服务,从不返回到控制器,所以无法打印咆哮消息。servicenot的回调数据返回给控制器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

您不以正确的顺序传递参数。 'httpService.postRequest(url,data,callback);''callback'是postRequest的第三个'参数'。从第三个参数开始,它与'function(url,data,contentType,callback)' - 'contentType'绑定。在你的httpService中使用this.postRequest = function(url,data,callback,contentType)**回调必须是第三个参数** –

+0

yes..now works.thank u – Jayashree

+0

很高兴帮助:)。 –

回答

1

正如我在评论中所说。

您不以正确的顺序传递参数。

您的callbackpostRequest的第三个参数。它与function(url, data, contentType, callback) - contentType结合。

因为在你的httpServicethis.postRequest = function(url, data,callback, contentType)

回调,是你的第三个参数使用这一定是第三个参数

变化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) { 

    }); 
}; 
相关问题